Interpolation and quoting
Scalars in strings
Inside double quotes Perl replaces a variable name with its value as the string is built. There is no format string to learn and no placeholder: the sigil is the marker.
my $user = "ada"; my $home = "/home/$user"; print "$user lives in $home\n";
perl demo.pl
ada lives in /home/ada
A backslash before the sigil stops the replacement and leaves the dollar sign in the text. That is how you print a literal amount such as a shell variable name:
print "cost: \$5\n"; # cost: $5 print "var: \$HOME\n"; # var: $HOME
perl demo.pl
cost: $5
var: $HOME
Braces disambiguate
Perl takes the longest valid name after the sigil. If a letter follows the variable, the parser thinks it belongs to the name and looks for a variable that does not exist. Wrap the name in braces to mark where it ends.
my $file = "app"; print "${file}s\n"; # apps, braces stop the name print "${file}_log\n"; # app_log
perl demo.pl
apps
app_log
Arrays in strings
An array interpolates as its elements joined by spaces, which is handy for messages but dangerous for machine-readable output. Control the separator with join and an explicit expression instead.
my @args = qw(--fast --quiet); print "args: @args\n"; # args: --fast --quiet print "n=", scalar @args, "\n"; # n=2
perl demo.pl
args: --fast --quiet
n=2
A single element keeps the dollar sigil and puts the index in square brackets, because the at sign alone would interpolate the whole array:
my @args = qw(--fast --quiet); print "first: $args[0]\n"; # first: --fast print "last index: $#args\n"; # last index: 1
perl demo.pl
first: --fast
last index: 1
Heredocs
For several lines of text the heredoc syntax is easier than a pile of quotes. Write two angle brackets and an identifier, then the text, then the identifier alone on a line to close it.
The quoted form <<'END' behaves like single quotes and interpolates nothing, which is what you want for SQL, shell scripts and templates.
my $name = "Ada"; my $text = <<"END"; Hello $name, this line is part of the string. END print $text;
perl demo.pl
Hello Ada,
this line is part of the string.
Escaping the sigil
When a string is mostly code and only partly data, quote the whole thing with q and interpolate nothing. The two approaches below build the same message; pick the one that keeps the sigils readable.
my $host = "db1"; print "ssh \$USER\@$host\n"; print qq{ssh \$USER\@$host\n};
perl demo.pl
ssh $USER@db1
ssh $USER@db1
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How does Perl know which part of a double-quoted string is a variable?
Answer
The sigil marks the start of a name, so there is no format string and no placeholder
Interpolation runs on the sigil, which is why
"$user"works without any extra markup. -
What does
print "cost: \$5\n";print?Answer
cost: $5
The backslash stops the interpolation and leaves the dollar sign in the text, without adding another character.
-
Why does
"${file}s"work when only$fileis declared?Answer
The braces mark where the name ends, so the s stays text
Without them Perl looks for
$files, which is a different variable; the braces end the name atfile. -
How does an array interpolate into a string, and what is the risk?
Answer
As its elements joined by spaces, which is fine for people but not for machine-readable output
The space separator is invisible in the string, so a value with a space becomes two fields;
joinmakes the separator explicit. -
What is
$#argsin a string?Answer
The index of the last element
$#gives the last index, one less than the element count thatscalar @argsreturns. -
What does the heredoc
<<'END'do differently from<<"END"?Answer
It interpolates nothing, like single quotes
The quoted form is the single-quoted literal over several lines, which is what SQL and shell scripts need.
-
What does this program print?
my $file = "app"; print "${file}log\n";Answer
applog
The braces end the variable name, and the rest of the word follows it as text.
-
What does this program print?
my @a = qw(x y z); print scalar @a, " ", $#a, "\n";Answer
3 2
Three elements make the count 3 and the last index 2, because indices start at zero.
-
What does this program print?
my $name = "Ada"; my $t = <<'END'; Hi $name END print $t;Answer
Hi $name
The quoted heredoc behaves like single quotes, so the dollar sign and the name stay as written.
-
What does this program print?
my @args = qw(a b); print "args: @args\n";Answer
args: a b
An array in a string interpolates as its elements joined by a space, in their order.
Article author: Arthur Isaev