- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

7.29 Quote and Quote-like Operators

While we usually think of quotes as literal values, in Perl they function as operators, providing various kinds of interpolating and pattern matching capabilities. Perl provides customary quote characters for these behaviors, but also provides a way for you to choose your quote character for any of them. In the following table, a {} represents any pair of delimiters you choose.

Customary  Generic        Meaning        Interpolates
    ”       q{}          Literal             no
    ""      qq{}          Literal             yes
    “      qx{}          Command             yes*
            qw{}         Word list            no
    //       m{}       Pattern match          yes*
            qr{}          Pattern             yes*
             s{}{}      Substitution          yes*
            tr{}{}    Transliteration         no (but see below)
    <<EOF                 here-doc            yes*
    * unless the delimiter is ”.

Non-bracketing delimiters use the same character fore and aft, but the four sorts of brackets (round, angle, square, curly) will all nest, which means that

q{foo{bar}baz}

is the same as

'foo{bar}baz'

Note, however, that this does not always work for quoting Perl code:

$s = q{ if($a eq "}") ... }; # WRONG

is a syntax error. The Text::Balanced module (from CPAN, and starting from Perl 5.8 part of the standard distribution) is able to do this properly.

There can be whitespace between the operator and the quoting characters, except when # is being used as the quoting character. q#foo# is parsed as the string foo, while q #foo# is the operator q followed by a comment. Its argument will be taken from the next line. This allows you to write:

s {foo}  # Replace foo
  {bar}  # with bar.

The following escape sequences are available in constructs that interpolate and in transliterations.

\t tab (HT, TAB)
\n newline (NL)
\r return (CR)
\f form feed (FF)
\b backspace (BS)
\a alarm (bell) (BEL)
\e escape (ESC)
\033 octal char (example: ESC)
\x1b hex char (example: ESC)
\x{263a} wide hex char (example: SMILEY)
\c[ control char (example: ESC)
\N{name} named Unicode character
\N{U+263D} Unicode character (FIRST QUARTER MOON)
The character following \c is mapped to some other character by converting letters to upper case and then (on ASCII systems) by inverting the 7th bit (0x40). The most interesting range is from '@' to '_' (0x40 through 0x5F), resulting in a control character from 0x00 through 0x1F. A '?' maps to the DEL character. On EBCDIC systems only '@', the letters, '[', '\', ']', '^', '_' and '?' will work, resulting in 0x00 through 0x1F and 0x7F. \N{U+wide hex char} means the Unicode character whose Unicode ordinal number is wide hex char. For documentation of \N{name}, see "Define character names for \N{named} string literal escapes" (charnames) in the Perl Library Reference Manual (Volume 1). NOTE: Unlike C and other languages, Perl has no \v escape sequence for the vertical tab (VT - ASCII 11), but you may use \ck or \x0b. (\v does have meaning in regular expression patterns in Perl, see 11.) The following escape sequences are available in constructs that interpolate, but not in transliterations.
\l          lowercase next char
\u          uppercase next char
\L          lowercase till \E
\U          uppercase till \E
\E          end case modification
\Q          quote non-word characters till \E
If use locale is in effect, the case map used by \l, \L, \u and \U is taken from the current locale. See "Perl locale handling (internationalization and localization)" (perllocale) in the Perl Unicode and Locales Manual. If Unicode (for example, \N{} or wide hex characters of 0x100 or beyond) is being used, the case map used by \l, \L, \u and \U is as defined by Unicode. All systems use the virtual "\n" to represent a line terminator, called a "newline". There is no such thing as an unvarying, physical newline character. It is only an illusion that the operating system, device drivers, C libraries, and Perl all conspire to preserve. Not all systems read "\r" as ASCII CR and "\n" as ASCII LF. For example, on a Mac, these are reversed, and on systems without line terminator, printing "\n" may emit no actual data. In general, use "\n" when you mean a "newline" for your system, but use the literal ASCII when you need an exact character. For example, most networking protocols expect and prefer a CR+LF ("\015\012" or "\cM\cJ") for line terminators, and although they often accept just "\012", they seldom tolerate just "\015". If you get in the habit of using "\n" for networking, you may be burned some day. For constructs that do interpolate, variables beginning with "$" or "@" are interpolated. Subscripted variables such as $a[3] or $href->{key}[0] are also interpolated, as are array and hash slices. But method calls such as $obj->meth are not. Interpolating an array or slice interpolates the elements in order, separated by the value of $", so is equivalent to interpolating join $", @array. "Punctuation" arrays such as @* are only interpolated if the name is enclosed in braces @{*}, but special arrays @_, @+, and @- are interpolated, even without braces. You cannot include a literal $ or @ within a \Q sequence. An unescaped $ or @ interpolates the corresponding variable, while escaping will cause the literal string \$ to be inserted. You'll need to write something like m/\Quser\E\@\Qhost/. Patterns are subject to an additional level of interpretation as a regular expression. This is done as a second pass, after variables are interpolated, so that regular expressions may be incorporated into the pattern from the variables. If this is not what you want, use \Q to interpolate a variable literally. Apart from the behavior described above, Perl does not expand multiple levels of interpolation. In particular, contrary to the expectations of shell programmers, back-quotes do NOT interpolate within double quotes, nor do single quotes impede evaluation of variables when used within double quotes.
ISBN 9781906966027Perl Language Reference ManualSee the print edition