Perl Scalars
A scalar is a single value: a number, a string, or a reference (covered later). Scalar variables begin with $. Perl does not ask you to choose between int, float or string: a scalar holds whatever you put in it, and Perl converts between numbers and strings as needed.
Declaring with my
Under use strict, every variable must be declared, normally with my. A declaration can include an initial value:
use strict;
use warnings;
my $product = "Coffee beans";
my $weight = 0.5; # kilograms
my $price = 12;
my $in_stock; # declared, but no value yet (undef)
print "$product: $weight kg for \$price\n";
print "Stock known? ", (defined $in_stock ? "yes" : "no"), "\n";Coffee beans: 0.5 kg for $12
Stock known? no
\$ prints a literal dollar sign inside a double-quoted string. A variable declared without a value holds the special value undef ("undefined"), which you can test with defined.
Assigning and reassigning
A scalar can be reassigned at any time, even to a different kind of value:
use strict;
use warnings;
my $value = 42;
print "value = $value\n";
$value = "forty-two";
print "value = $value\n";
$value = 4.2e1; # scientific notation
print "value = $value\n";
my ($x, $y) = (3, 4); # declare and assign two at once
($x, $y) = ($y, $x); # swap without a temporary variable
print "x = $x, y = $y\n";value = 42
value = forty-two
value = 42
x = 4, y = 3
The line ($x, $y) = ($y, $x); is a list assignment: Perl evaluates the right side first, then assigns the values in order, which makes swapping trivial.
Numbers and strings convert automatically
Perl chooses the meaning of a scalar from the operator you use on it, not from the value itself. Numeric operators like + treat values as numbers; string operators like . (concatenation) treat them as strings:
use strict;
use warnings;
my $text = "10";
my $num = 5;
print $text + $num, "\n"; # numeric addition
print $text . $num, "\n"; # string concatenation
print "3 apples" * 1, "\n";15
105
3
Argument "3 apples" isn't numeric in multiplication (*) at /app/output.s line 9.
When a string starts with a number, Perl uses that leading number, so "3 apples" becomes 3. use warnings points out that the string was not purely numeric, which is usually a sign of a bug.
Naming conventions
| Style | Example | Used for |
|---|---|---|
| lower_snake_case | $file_name |
ordinary variables (most common) |
| UPPER_CASE | $MAX_RETRIES |
values that should not change |
| short names | $i, $n |
loop counters, tiny scopes |
Perl also has many special variables with punctuation names, such as $_ (the default variable), $0 (the script name) and $@ (the last error). You will meet them one at a time.
Constants
For values that must never change, use the constant pragma. Constants have no sigil:
use strict;
use warnings;
use constant PI => 3.14159;
use constant TAX_RATE => 0.2;
my $radius = 2;
print "Area: ", PI * $radius ** 2, "\n";
print "Tax on 50: ", 50 * TAX_RATE, "\n";Area: 12.56636
Tax on 50: 10
Note: Constants do not interpolate in strings:
"PI is PI"prints the wordPItwice. Use concatenation or a list in
Test yourself
What value does my $x; give $x?
- 0
- an empty string
undef- a syntax error
What does "7" . 3 produce?
- 10
- "73"
- 21
- an error
How do you swap $x and $y in Perl?
swap($x, $y);($x, $y) = ($y, $x);$x <=> $y;$x = $y = $x;