"Perl for Perl Newbies" - Part 4Alternate Forms for Writing Stringsq{}, qq{} and Friends

3.1. q{}, qq{} and Friends

Customary Generic Meaning Interpolates
'' q{} Literal No.
"" qq{} Literal Yes
`` qx{} Command Yes (unless the delimiter is '')
(none) qw{} Word List No
// m{} Pattern Match Yes (unless the delimiter is '')
(none) qr{} Declaration of a Regex Pattern Yes (unless the delimiter is '')
(none) s{}{} Substitution Yes (unless the delimiter is '')
(none) tr{}{} Transliteration No

What it means, is that you can write an interpolated string as qq followed by a matching wrapping character, inside which the string can be placed. And likewise for the other strings. Here are some examples:

#!/usr/bin/perl -w

use strict;
use warnings;


my $h = q{Hello There};
print qq|$h, world!\n|;

my $t = q#Router#;
my $y = qq($h $h $h $t);
$y =~ s!Hello!Hi!;
print qq#$y\n#;

my @arr = qw{one two three};
for my $i (0 .. $#a)
{
    print "$i: $arr[$i]\n";
}

The output of this is:

Hello There, world!
Hi There Hello There Hello There Router
0: one
1: two
2: three

As one can see, the wrapping characters should match assuming they are a left/right pair ({ to } etc.).


Written by Shlomi Fish