There are several things to watch out for with interpolation, although once you know about them, you hardly ever do them by mistake.
Putting a variable name next to valid identifier text. Perl finds the longest valid variable name and doesn't care if it is previously defined. You can set off the variable name portion with braces to be explicit:
my $p = 'p';
print "Mind your $ps and qs\n"; # $ps, not $p
print "Mind your ${p}s and qs"; # now its $p
Now, in that example, I forgot the apostrophe. If I add it, I have another problem since the apostrophe used to be the package separator from the old days and it still works. The braces work there too:
my $p = 'p';
print "Mind your $p's and q's\n"; # $p::s, not $p
print "Mind your ${p}'s and q's"; # now its $p
Perl can also interpolate single element accesses to hashes and arrays, so putting indexing characters next to a variable name might do something you don't want:
print "The values are $string[$foo]\n"; That's the element at index $foo
print "The values are $string{$foo}\n"; That's the value for the key $foo
When you want an email address in a string, you might forget Perl interpolates arrays. Perl used to make that a fatal error unless you escaped the @
:
print "Send me mail at [email protected]\n"; # interpolates @example
print "Send me mail at joe\@example.com\n";
Since Perl uses the backslash to escape some characters, you need to double up those when you want a literal one:
print "C:\real\tools\for\new\work"; # not what you might expect
print "C:\\real\\tools\\for\\new\\work"; # kinda ugly, but that's life
print "C:/real/tools/for/new/work"; # Windows still understands this
Despite these minor gotchas, I really miss the ease with which I can construct strings in Perl if I have to use another language.