Perl Language Basic interpolation


Interpolation means that Perl interpreter will substitute the values of variables for their name and some symbols (which are impossible or difficult to type in directly) for special sequences of characters (it is also known as escaping). The most important distinction is between single and double quotes: double quotes interpolate the enclosed string, but single quotes do not.

my $name = 'Paul';
my $age = 64;
print "My name is $name.\nI am $age.\n"; # My name is Paul.
                                         # I am 64.


print 'My name is $name.\nI am $age.\n'; # My name is $name.\nI am $age.\n

You can use q{} (with any delimiter) instead of single quotes and qq{} instead of double quotes. For example, q{I'm 64} allows to use an apostrophe within a non-interpolated string (otherwise it would terminate the string).


print qq{$name said: "I'm $age".}; # Paul said: "I'm 64".
print "$name said: \"I'm $age\"."  # Paul said: "I'm 64".

do the same thing, but in the first one you do not need to escape double quotes within the string.

If your variable name clashes with surrounding text, you can use the syntax ${var} to disambiguate:

my $decade = 80;
print "I like ${decade}s music!"  # I like 80s music!