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

3.1. q{}, qq{} and Friends

``qx{}CommandYes (unless the delimiter is '')
(none)qw{}Word ListNo
//m{}Pattern MatchYes (unless the delimiter is '')
(none)qr{}Declaration of a Regex PatternYes (unless the delimiter is '')
(none)s{}{}SubstitutionYes (unless the delimiter is '')

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