Home » Howtos » Using perl » Interpolation |
This page shows how variable interpolation works in Perl. Interpolation, meaning "introducing or inserting something", is the name given to replacing a variable with the value of that variable.
In Perl, any string that is built with double quotes (or something meaning double quotes) will be interpolated. That is, any variable or escaped char that appears within the string will be replaced with the value of that variable. Here is a small example:
#!/usr/bin/perl use strict; use warnings; my $apples = 4; print "I have $apples apples\n";
This would have the following output:
I have 4 apples
In perl, when you print an array inside double quotes, the array elements are printed with spaces between. The following program provides a small example:
#!/usr/bin/perl use strict; use warnings; my @friends = ('Margaret', 'Richard', 'Carolyn', 'Rohan', 'Cathy', 'Yukiko'); print "Friends: @friends\n";
This program produces the following output:
Friends: Margaret Richard Carolyn Rohan Cathy Yukiko
This is very helpful when debugging.
The function qq() works just like double quotes, but makes it easy to put double quotes in your string:
#!/usr/bin/perl use strict; use warnings; my $apples = 4; print qq(I "have" $apples apples\n);
This would produce:
I "have" 4 apples
Here documents work in exactly the same way. If the end token of the here document (e.g. <<"EOT") is surrounded in double quotes, then variables in the here document will be interpolated:
#!/usr/bin/perl use strict; use warnings; my $apples = 4; my $oranges = 7; my $pears = 3; print <<"EOT"; My fruit list: $apples apples $oranges oranges $pears pears EOT
The output of this program is:
My fruit list: 4 apples 7 oranges 3 pears
Single quoted strings do not interpolate variables or most escaped values, such as \n, (however \' is the exception). Single quotes are helpful when you want to include a $ or a % or any other special character in your output:
#!/usr/bin/perl use strict; use warnings; print 'Those strawberries cost $2.50';
This would produce the output you would want:
Those strawberries cost $2.50
The function q() works the same as single quotes, except it makes it easier to include a single quote in your data:
#!/usr/bin/perl use strict; use warnings; print q(Bob's strawberries cost $2.50);
This would produce:
Bob's strawberries cost $2.50
In the same way, variables in here documents, where the end token is surrounded with single quotes, are not interpolated:
#!/usr/bin/perl use strict; use warnings; my $apples = 4; my $oranges = 7; my $pears = 3; print <<'EOT'; My fruit list: $apples apples $oranges oranges $pears pears EOT
This would produce:
My fruit list: $apples apples $oranges oranges $pears pears
When defining a hash, the key of the hash is a string. For example:
my %pets = ( 'bob' => 'cat', 'tamba' => 'dog', 'ceasar' => 'horse', );
The key (on the left) does not have to be quoted unless it contains spaces, or characters that may be interpolated. For example, the above hash could be written as:
my %pets = ( bob => 'cat', tamba => 'dog', ceaser => 'horse', );
But neither of the following keys are valid:
my %pets = ( a dog => 'tamba', dollars$ => '40', );
If you are constructing or printing a string that contains no variables, then use single quotes or q(). This makes your code easier to read if there are dollar signs or other special characters in your text.
If you are printing a lot of data use a here document. It will make your code easier to read.
If you need to print a variable amongst some text, use double quotes or qq(). This is much tidier than repeated uses of the '.' concatenation operator.
Sometimes you will find that you do need to use either backslashes or concatenation operators. For example, if you want to print a dollar sign and then an amount in a variable, you could use either:
my $amount = 40.00; print 'The amount is $' . $amount;
or:
my $amount = 40.00; print "The amount is \$$amount";
The first example is probably the better one to use as $$ is the perl variable for process id.
perldoc perlop See the sections Comma Operator Quote and Quote-like Operators Gory details of parsing quoted constructs perldoc -q "quote.*strings"