Scalars & Basic Data Types
💲 Scalars & Basic Data Types
$, the two data types a scalar actually stores (strings and numbers), string interpolation, and the three ways Perl handles variable scope.
🏷️ The $ Sigil
A scalar — a single value, as opposed to a list of values — is always named with a leading $:
This is a genuinely distinctive design choice worth internalizing early: $ always means "one scalar value," @ (Chapter 3) always means "an array," % (Chapter 4) always means "a hash." The sigil tells you the variable's kind at a glance, everywhere in a script — even, as you'll see in Chapter 3, when reading a single element out of an array.
🔤 Scalar Values: Strings & Numbers
A scalar can hold a string, a number, or (Chapter 9) a reference — Perl doesn't require declaring which up front, and converts between string and numeric interpretations automatically depending on context.
Single-quoted strings are literal — nothing inside them is interpreted except an escaped quote or backslash. Double-quoted strings are interpolated — variables and certain escape sequences (\n, \t) are expanded.
🧵 String Interpolation
Interpolation directly inside a double-quoted string is idiomatic Perl and generally preferred over concatenation with . — it reads closer to the final output and avoids a chain of . operators for anything with more than one or two variables.
Inside double quotes, $ always signals "interpolate a variable here" — a genuine dollar sign has to be escaped as \$, or the string switched to single quotes if nothing else in it needs interpolating.
🔢 Numeric Operators & String-Number Duality
This last line is a real, distinctive Perl behavior: a string that "looks like" a number is automatically converted when used in a numeric operation. It's convenient for data read from files or user input (always strings) — but relying on it with genuinely non-numeric strings produces 0 and, with use warnings active, a visible "abc" isn't numeric warning rather than a silent wrong answer.
🔭 my, our, and local — Scoping
| Keyword | Scope | Use case |
|---|---|---|
my | Lexical — visible only within the enclosing block | The default — almost every variable you declare should be my |
our | Package-scoped — shared across the whole package/file | A genuine global, still requires declaring under use strict |
local | Dynamic — temporarily overrides a global's value for the current block only, then restores it | Rare; mostly for temporarily changing a special/global variable |
local is easy to confuse with my since both look like they're "declaring a new variable" — but local only ever works on an existing package/global variable, temporarily saving and restoring its value. Real Perl code reaches for my for the overwhelming majority of variables; local is a specialized tool for a specific, fairly rare situation.
Sigils: Perl vs. Python/JavaScript
Python and JavaScript variables carry no prefix at all — name = "Ada", let name = "Ada" — the variable's kind is inferred purely from context and its assigned value. Perl's sigil is visible everywhere the variable is used, not just where it's declared, which is part of why Perl code can look unfamiliar at first glance to someone coming from those languages — but it also means you can tell at a glance, mid-expression, whether you're looking at a single scalar, a whole array, or a whole hash.
$ sigil
Marks a scalar — a single string, number, or reference.
Quoting
Single quotes are literal; double quotes interpolate variables and escape sequences.
String-number duality
A numeric-looking string converts automatically in numeric context.
my / our / local
Lexical (default), package-global, and temporary-override scoping.
💻 Coding Challenges
Challenge 1: A Personalized Greeting
Declare two scalars, $name and $city, and use string interpolation in a single say call to print "Ada lives in London." (using whatever values you assign).
Challenge 2: Single vs Double Quotes
Given my $x = 5;, write one say statement using single quotes and one using double quotes, both attempting to print the phrase The value is $x — show the actual output of each, and explain why they differ.
Challenge 3: A Simple Calculator
Declare two numeric scalars and use say to print their sum, difference, product, and quotient, each on its own line, each labeled (e.g. "Sum: 15").
🎯 What's Next
Next chapter: Arrays & Lists — the @ sigil, array operations, and list vs. scalar context.