Pick Your Own Quotes
Although we usually think of quotes as literal values, in Perl they
function more like operators, providing various kinds of
interpolating and pattern-matching capabilities. Perl provides the
customary quote characters for these behaviors, but it also provides
a more general way for you to choose your quote character for any of
them. In Table 2-7, any nonalphanumeric,
nonwhitespace delimiter may be used in place of /. (The newline and space characters are
no longer allowed as delimiters, although prehistoric versions of
Perl once allowed this.)
Table 2-7. Quote constructs
| Customary | Generic | Meaning | Interpolates |
|---|---|---|---|
''
|
q//
| Literal string | No |
""
|
qq//
| Literal string | Yes |
``
|
qx//
| Command execution | Yes |
()
|
qw//
| Word list | No |
//
|
m//
| Pattern match | Yes |
s///
|
s///
| Pattern substitution | Yes |
tr///
|
y///
| Character translation | No |
""
|
qr//
| Regular expression | Yes |
Some of these are simply forms of “syntactic sugar” to let you avoid putting too many backslashes into quoted strings, particularly into pattern matches where your regular slashes and backslashes tend to get all tangled.
If you choose single quotes for delimiters, no variable interpolation is done even on those forms that ordinarily interpolate. If the opening delimiter is an opening parenthesis, bracket, brace, or angle bracket, the closing delimiter will be the corresponding closing character. (Embedded occurrences of the delimiters must match in pairs.) Examples:
my $single = q!I said, "You said, 'She said it.'"!; my $double = qq(Can't ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access