http://www.perlmonks.org?node_id=943683

perlop contains a rather lengthy section, Quote and Quote-like Operators, containing a detailed explanation of these related constructs. You may wish to review Regexp Quote-Like Operators, Quote-Like Operators, and Gory details of parsing quoted constructs, all in the same document. This node contains an extremely brief summary, with examples.

In the listings that follow, #: indicates the output of the preceding code.

Quoting String Literals

In the most common case, we wish to store or print a simple string literal as is. The single quotes merely limit the string:

say 'Cwm fjordbank glyphs vext quiz.'; #:Cwm fjordbank glyphs vext quiz.

This works well enough except when the literal string must contain, literally, the single-quote character itself. Then we may escape the troublesome characters or use generic quotes:

say 'a\'b\'c'; #:a'b'c say q|a'b'c|; #:a'b'c

If we need just a literal backslash then we need to escape it, too; otherwise perl will think we're trying to escape the trailing delimiter:

say '\a'; #:\a say '\\'; #:\ say q|\a|; #:\a say q|\\|; #:\

It's irrelevant which delimiters we use in a generic quoting construct except that, obviously, we're better off choosing delimiters that aren't in our string. Other than that, it's a matter of style. Different programmers prefer different delimiters:

say q|a'b'c|; # || say q(a'b'c); # () say q[a'b'c]; # [] say q<a'b'c>; # <> say q/a'b'c/; # // say q*a'b'c*; # ** #:a'b'c

Sometimes we just use generic quotes for clarity, even when plain single quotes would work as well:

say ''; # empty string? say q||; # clearer say ' '; # single space? say q| |; # clearer

Interpolation

Sometimes we want to mix in variable values or escape sequences; then we use interpolation. The double quotes are the most common:

my $foo = 'bar'; say "a-$foo-b"; #:a-bar-b

The previous issues come up with interpolating quotes, too; if we want to include a double-quote character, literally, we need to use generic interpolating quotes:

say qq|This\t('"')\tis a double-quote.|; #:This ('"') is a double-quote.

And again, we can use any delimiters we like.

Common escape sequences

\t tab \n newline \r carriage return \b backspace say qq|a-\t-b-\n-c|; #:a- -b- #:-c

Many, many other escape sequences are possible; see perlop.

Heredocs

As a last resort, peculiar or long strings can be quoted with a heredoc construct:

say <<'HERE'; say qq|This\t('"')\tis a double-quote.|; # my $string = '\a\s\b'; HERE #:say qq|This\t('"')\tis a double-quote.|; #:# my $string = '\a\s\b'; #:

If you don't single-quote the word 'HERE' in the example above, the heredoc will interpolate, much like double quotes.

Heredocs are ugly and confusing, best avoided... unless there's no better way.

Quoting a List

We may want to create a list of quoted strings, perhaps to store in an array. These statements are equivalent:

my @ary = ( 'Cwm', 'fjordbank', 'glyphs', 'vext', 'quiz' ); my @ary = qw| Cwm fjordbank glyphs vext quiz |;

Again, any delimiters may be used with the quote word operator. But please don't make foolish choices.

Other Quote-like Constructs

Regular expressions may be considered quoting, interpolating constructs. Before any matching is done, the contents of a regex will be expanded much as if it were enclosed in double quotes:

my $regex = 'a|b|c'; my $string = 'xxbxx'; if ( $string =~ /$regex/ ) { say 'TRUE' } else { say 'FALSE' }; #:TRUE

Note that in the example above, $regex was assigned the value obtained by simple single-quoting a literal a|b|c. It's also possible to use two other constructs, quote regex and quotemeta:

my $regex = qr/a|b|c/; my $string = 'xxbxx'; if ( $string =~ /$regex/ ) { say 'TRUE' } else { say 'FALSE' }; #:TRUE

Benefits to using qr// instead of q// or qq// include compile-time checking and possible optimization.

Using quotemeta on a string escapes the characters that might be special:

my $regex = '\s'; my $string = '\a\s\b'; if ( $string =~ /$regex/ ) { say 'TRUE' } else { say 'FALSE' }; #:FALSE $regex = quotemeta $regex; if ( $string =~ /$regex/ ) { say 'TRUE' } else { say 'FALSE' }; #:TRUE

Please see also perlreref, perlretut, perlre, perlrequick.

If you've used backticks to do a system call, you might like to know that there is also a generic backtick operator:

say `date`; say qx|date|; #:Thu Dec 15 01:19:30 PST 2011

Summary

Thanks

  • davido for pointing out some benefits to qr//.
  • ambrus for pointing out some esoteric benefits to qr//.
  • setebos for making clear the need.

Changes

Suggestions for improvements are welcome and will be incorporated.

2011-12-15:
  • new