If you're sick and tired of backslashing double quote signs, and you want to know why you can't print "japhy@pobox.com" without Perl yelling at you, this article will clear things up for you. We'll straighten out the uses of Perl's varying quoting operators, and learn about interpolation.
Perl gives you two quoting contexts, and from these two, there are minor
variations. The two parent contexts are single-quoted context and
double-quoted context. In single-quoted context, there is no
interpolation done at all. In double-quoted context, scalar variables (and
array elements and hash values) and arrays (and array slices and hash slices)
are interpolated. The term interpolation means that Perl will "expand"
or "evaluate" an expression. In single-quoted context, the following string
is exactly what you see:
He said "You owe $10.00 for Joe's, too!" to soldier m\\\n12-@N-6-%foo.
In double quoted context, the variable $10 would be evaluated (this
would be the tenth remembered submatch of a previously matched regular
expression, the sequence \\ indicates a literal backslash, the
sequence \n indicates a newline character, and the variable
@N would be evaluated. In double-quoted context, interpolated arrays
expand to a string of their elements, separated by the value of the $"
variable.
However, there is only a single way to get this true single-quoted context, and
that is with here-docs to be described later. When using single quotes,
the backslash (\) comes into play, and there are two places where
minor interpolation comes into play; these are to backslash the character
being used for quoting, and the other is to backslash the backslash itself,
so that if you want a literal backslash as te last character in a quoted
string, you can do that:
$str = '\n is a literal \ and a n';
$str = 'here is a quote \' <--';
$str = 'this ends in a slash\\';
$str = 'a slash and a quote\\\'';
Perl sympathizes with the plight of the frequency of the apostrophe (single
quote) in many languages, and acknowledges that you might really want to put
apostrophes in a single-quoted string. So Perl offers the q()operator -- not function, operator. q() takes any set of
delimiters (some of which pair as left and right), and treats the string as a
single-quoted string:
$str = q(Don't do that!);
$str = q/I said, "You're in for it!"/;
$str = q{ for (1 .. 10) { print "$_\n"; } };
$str = q~% cd \~jeffp/pub_html/~;
You should notice something important from these examples: with paired
delimiters {...}, you can nest the delimiters -- meaning, Perl
will allow for balanced pairs of these left and right delimiters. These can be
the pairs (), [], {}, and <>. If
there will be be an uneven number of these paired delimiters (example shown
below), the offending delimiter characters must be backslashed.
The caveat of backslashes and directory paths (under Win32 systems) is
mentioned later.
Perl offers another operator, qw(), which works in a way very similar
to split(' ', q()). That is, it takes the expression you give it,
treats it like a single-quoted string (q() rules of interpolation are
honored) and then uses a special case of the split() function on the
string. split(' ') means that leading and trailing whitespace is
discarded, and then the resulting string is split on chunks of whitespace. It
is the same as the following, but what follows is more complicated than the
simple use of split(' '):
split /\s+/, ($string =~ /(\S.*\S)/s)[0];
Important note: split(' ') is NOT the same as split(/ /).
The former exists as a special case (and can be written as split(" ")
as well), while the latter splits a string on the regex of a single space.
In double-quoted context, certain Perl expressions (any scalar value or slice
of an array or hash) is expanded to its value, and specific sequences (noted
by a beginning backslash) are expanded to their value. Here is a table of
these sequences:
Sequence
Value
\t
tab
\n
newline
\r
return
\f
form feed
\b
backspace
\a
alarm (bell)
\e
escape
\NNN
octal char (N is octal digit)
\xNN
hex char (N is hex digit)
\cN
control-char
\$
literal $
\@
literal @
\l
lowercase next char
\u
uppercase next char
\L
lowercase until \E
\U
uppercase until \E
\Q
backslash regex metachars until \E
\E
end case (or \Q) modification
For double-quoted context, Perl offers regular double quotes (""), or
the qq()operator -- not function, operator. It has the same
delimiter rules as q() does; specifically, if you use pairing
delimiters, you can nest them, and that other delimiters need to be backslashed
if they are to occur in the string as the literal character, and that in a
sequence of two adjacent backslashes, a literal backslash is produced:
$str = qq(Once (upon (a (time) ) ) I used Lisp.);
$str = qq/Bad way to print a directory: \/usr\/local\/bin\//;
$str = "Hi, $name. Your friends are @friends.\n";
$str = qq,\\\ is a backslash followed by a space,;
$str = qq(That'll be \$10.00 :\));
Another quoting construct that uses double-quoted context are backticks,
`...`, available also through use of the qx() operator.
The backticks execute their contents as a system command, and return what the
command sent to standard output. In scalar context, multiline output is stored
in a scalar, each line ending in a newline character; in list context, the
output is stored one line per list element, with each element ending with a
newline. This quoting construct uses double-quoted interpolation, so you can
send Perl variables to a command and they will be interpolated to their values
beforehand.
Earlier, I said there was only one way to get true single-quoted context, where
what you type is what you get. That way is using here-docs, which is a quoting
construct borrowed from shell scripting.
The simplest form of a here-doc is as follows. Note there is no space between
the << and the here-doc label. The label must appear exactly
as written at the end of a here-doc, and must be followed by a newline.