Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the legal delimiters for Perl 5's pick-your-own-quotes operators?

Tags:

quotes

perl

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).

like image 315
Kodiologist Avatar asked Apr 25 '17 17:04

Kodiologist


1 Answers

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 the m.

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).

like image 142
zdim Avatar answered Oct 20 '22 19:10

zdim