Scalars & Basic Data Types

Perl Fundamentals — Scalars & Basic Data Types
Perl Fundamentals
Course 1 · Chapter 2 · Scalars & Basic Data Types

💲 Scalars & Basic Data Types

Every Perl variable name is prefixed with a sigil — a symbol indicating what kind of variable it is, not what type of value it holds. This chapter covers the scalar sigil $, 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 $:

my $name = "Ada"; my $age = 28;

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.

my $count = 42; my $price = 19.99; my $big = 1_000_000; # underscores allowed for readability, ignored by Perl my $single = 'Raw text, no interpolation: $count'; my $double = "Interpolated: $count";

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

my $name = "Ada"; say "Hello, $name!"; # Hello, Ada! — $name expanded directly inside the string my $greeting = "Hi, " . $name . "!"; # concatenation with the . operator $greeting .= " Welcome."; # .= appends in place

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.

⚠ A Literal $ Inside a Double-Quoted String Needs Escaping
say "Price: $50"; # tries to interpolate a variable named $5, then a literal "0" — not what you want say "Price: \$50"; # Price: $50 — the backslash escapes $, forcing it to be literal

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

say 10 + 5; # 15 say 10 - 5; # 5 say 10 * 5; # 50 say 10 / 5; # 2 say 2 ** 8; # 256 — exponentiation say 10 % 3; # 1 — modulo say "10" + 5; # 15 — the STRING "10" is auto-converted to a number in numeric context

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

KeywordScopeUse case
myLexical — visible only within the enclosing blockThe default — almost every variable you declare should be my
ourPackage-scoped — shared across the whole package/fileA genuine global, still requires declaring under use strict
localDynamic — temporarily overrides a global's value for the current block only, then restores itRare; mostly for temporarily changing a special/global variable
our $VERSION = "1.0"; # a package-wide global sub show_version { local $VERSION = "1.0-debug"; # temporarily overrides $VERSION... inner_function(); # ...and inner_function() sees the overridden value too } # ...restored back to "1.0" automatically once this block ends
💡 When in Doubt, Use my

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).

→ Solution

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.

→ Solution

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").

→ Solution

🎯 What's Next

Next chapter: Arrays & Lists — the @ sigil, array operations, and list vs. scalar context.