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.

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

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

  3. Why does "${file}s" work when only $file is 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 at file.

  4. 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; join makes the separator explicit.

  5. What is $#args in a string?

    Answer

    The index of the last element

    $# gives the last index, one less than the element count that scalar @args returns.

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

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

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

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

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