The T-Files


Mon, 08 Mar 2004

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

The True Show

Last Wednesday I joined Natasha (and Marek, and Roberto, and about 60 other foreigners) for the recording of a segment of SMAPxSMAP, so if you have a chance to see Fuji TV tonight at ten, by all means do, as you might catch a glimpse of yours truly.

Part of SMAPxSMAP is The True Show, in which a celebrity has to disclose a well-kept secret. In this case it was SMAP member Tsuyoshi Kusanagi, who has also become known as Choran Kang since he started his own Korean language TV show three years ago. He has become very popular in Korea, made a few albums, and promised the prime minister to make a movie (which, surprise, surprise, happens to be premiering these days). The confession he made to show master John Kabira (speaks native American, native Japanese, DJs, comments play-by-play on the Japanese national soccer team for TV and video games) was that he did not speak Korean at all in the beginning, but just memorised the pronunciations.

TV shows all over the world work with premeditated cheers and jeers from audiences, but Japan takes it a step further. Not only was the whole studio audience paid to be there on Wednesday, and every one of their reactions carefully rehearsed (so that a three minute segment took four hours to shoot), the audience also consisted wholly of foreigners, most of them could not follow the confessions they were supposed to spontaneously react to. Two of us even had to shout scripted questions. Note the ironic twist that made Kusanagi confess about faking an interview in a language he did not understand to a jury that could not understand him either.