perlop provides various examples of the delimiters you can use for q
, qq
, s
, etc., including '
, /
, #
, and {
(closing with }
instead of itself). But what exactly are the rules for what's allowed? I notice that you can't use, for example, whitespace or a letter (edit: a letter is allowed if you put whitespace after the operator name).
We have in Regex Quote-Like Operators in perlop under m/PATTERN/msixpodualngc
With the
m
you can use any pair of non-whitespace (ASCII) characters as delimiters
[...]
When using a delimiter character valid in an identifier, whitespace is required after them
.
So if you want to use a word character as the delimiter, you'll need to separate it from the operator with whitespace. This applies to both regex and non-regex uses.
$ perl -le'print qq afooa'
foo
The only one for which you don't need m
is the solidus (/
).
The only one for which you don't need q
is the apostrophe ('
).
The only one for which you don't need qq
is the quotation mark ("
).
The only one for which you don't need qx
is the grave accent (`
).
Clearly, quoted characters that are the same as the chosen delimiter must be escaped.
A useful read is Gory details of parsing quoted constructs (perlop)
A simplified straight rule for general quoting can be found in Programming Perl, under Pick Your Own Quotes in Chapter 2 Bits and Pieces (3rd Ed)
... any nonalphanumeric, nonwhitespace delimiter may be used
in reference to the table much like the one in perlop.
While this comes straight from Camel note that with the use of space between the operator and the string we can also use alphanumeric delimiters, as shown in the beginning. And under use utf8;
Unicode may be used as well, thanks to ysth.
Thanks to ikegami for comments and edit.
Since the question is about the exact rules I'd like to note that I couldn't find a single comprehensive statement in the docs. While the first quote here is complete, it refers to Regex uses. The preceding section in perlop
on general Quote-Like operators does not specify the delimiters (while the same rule does apply).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With