The thousand and one reasons to love Perl: [2] String literals and interpolation
Perl (Practical Extracting and Reporting Language) was originally designed to work with text, and as such has plenty of nice features to work with strings. A very basic element is how you can specify string literals (constants) in your program source code, and Perl offers unmatched flexibility and convenience here. Those are simple things, sure, but why not get the simple things right?
Simple string literal
Well, every language can do that...
my $foo = 'A string';
String literal that contains quotes
If you want to include the quote character in your string, you usually have to escape it. Otherwise the parser will think it terminates the literal string. You can do that in Perl, but you can also simply choose different quote characters that are not contained in the string. With the qx operator you are fairly free in choosing the delimiter.
my $foo = q{A string with ' " quotes};
$foo = q[blah blah];
$foo = q=blah blah=;
Simple multi-line string literal
Many languages do not allow you to spread a string literal over more than one line. Perl does.
my $foo = 'A string with two lines';
Very long multi-line string literal
There is an additional syntax called here-docs that is specifically designed for
long multi-line strings which can also include all kinds of quote characters, that you would normally have to escape. It starts with <<MARK where MARK is the end-of-string mark of your liking. The string starts on the next line and continues until you put the MARK on a line by itself.
my $foo = <<RAVEN; Once upon a midnight dreary, while I pondered weak and weary, Over many a quaint and curious volume of forgotten lore, While I nodded, nearly napping, suddenly there came a tapping, As of some one gently rapping, rapping at my chamber door. `'Tis some visitor,' I muttered, `tapping at my chamber door - Only this, and nothing more.' RAVEN
Interpolation
Just like a command shell (but unlike most other programming languages) Perl allows for convenient interpolation of variables within strings:
my $name = 'Tyler Durden'; my $greeting = "Hello, $name!"; # note the double quotes my @numbers = qw( one two three four five ); # nifty way to quote an array of words my $foo = "I can count: @numbers"; # interpolate the array $foo = "3: $numbers[2]"; # or just an array element $foo = "even: @numbers[1,3], odd: @numbers[0,2,4] "; # or some array elements



