Recall that when we discussed double-quotes strings
(see Double-quoted Strings), we noted that we had to backslash the
$
character (e.g., "\$"
). Now, we discuss the reason that
this was necessary. Any scalar variable, when included in a
double-quoted string interpolates.
Interpolation of scalar variables allows us to insert the value of a scalar variable right into a double-quoted string. In addition, since Perl largely does all data conversion necessary, we can often use variables that have integer and float values and interpolate them right into strings without worry. In most cases, Perl will do the right thing.
Consider the following sample code:
use strict; my $friend = 'Joe'; my $greeting = "Howdy, $friend!"; # $greeting contains "Howdy, Joe!" my $cost = 20.52; my $statement = "Please pay \$$cost.\n"; # $statement contains "Please pay $20.52.\n" my $debt = "$greeting $statement"; # $debt contains "Howdy, Joe! Please pay $20.52.\n"
As you can see from this sample code, you can build up strings by placing scalars inside double-quotes strings. When the double-quoted strings are evaluated, any scalar variables embedded within them are replaced with the value that each variable holds.
Note in our example that there was no problem interpolating
$cost
, which held a numeric scalar value. As we have discussed,
Perl tries to do the right thing when converting strings to numbers and
numbers to strings. In this case, it simply converted the numeric value
of 20.52
into the string value '20.52'
to interpolate
$cost
into the double-quoted string.
Interpolation is not only used when assigning to other scalar
variables. You can use a double-quoted string and interpolate it in any
context where a scalar expression is appropriate. For example, we could
use it as part of the print
statement.
#!/usr/bin/perl use strict; use warnings; my $owner = 'Elizabeth'; my $dog = 'Rex'; my $amount = 12.5; my $what = 'dog food'; print "${owner}'s dog, $dog, ate $amount pounds of $what.\n";This example produces the output:
Elizabeth's dog, Rex, ate 12.5 pounds of dog food.
Notice how we are able to build up a large string using four variables,
some text, and a newline character, all contained within one
interpolated double-quoted string. We needed only to pass one
argument to print
! Recall that previously (see Printing Numeric Literals) we had to separate a number of scalar arguments by
commas to pass them to print. Thus, using interpolation, it is very
easy to build up smaller scalars into larger, combined strings. This is
a very convenient and frequently used feature of Perl.
You may have noticed by now that we did something very odd with
$owner
in the example above. Instead of using $owner
, we
used ${owner}
. We were forced to do this because following a
scalar variable with the character '
would confuse
Perl. 1 To make it
clear to Perl that we wanted to use the scalar with name owner
,
we needed to enclose owner
in curly braces ({owner}
).
In many cases when using interpolation, Perl requires us to do this.
Certain characters that follow scalar variables mean something special
to Perl. When in doubt, however, you can wrap the name of the scalar in
curly braces (as in ${owner}
) to make it clear to Perl what you
want.
Note that this can also be a problem when an interpolated scalar variable is followed by alpha-numeric text or an underscore. This is because Perl cannot tell where the name of the scalar variable ends and where the literal text you want in the string begins. In this case, you also need to use the curly braces to make things clear. Consider:
use strict; my $this_data = "Something"; my $that_data = "Something Else "; print "_$this_data_, or $that_datawill do\n"; # INVALID: actually refers # to the scalars $this_data_ # and $that_datawill print "_${this_data}_, or ${that_data}will do\n"; # CORRECT: refers to $this_data and $that_data, # using curly braces to make it clear