Quick Tip #25: Very literal Quoting

Perl 6 has a basic quoting mechanism that is as literal as it can be. It doesn’t escape anything and doesn’t interpolate anything. The Q does nothing special so you get a string as close to exactly what you see:

$ perl6
> Q/Single quote delimiter with \/; # no escaping
Single quote delimiter with \
> Q/This is about $var./ # no interpolation
This is about $var.
> Q"This is still about $var." # still no interpolation
This is still about $var.
> Q"C:\\This\is\a\path" # no \ proliferation!

It does have on interesting feature. It knows how to count balanced delimiters. If I use one of the paired characters, I can have the opening version in the literal string as long as the open and closing bits are balanced (so, every opener has a closer):

> Q< I have some << inside >> >
 I have some << inside >>

If I don’t have balanced thingys, I have a problem:

> Q< I have some << inside  >
===SORRY!=== Error while compiling:
Couldn't find terminator > (corresponding < was at line 1)
at line 2
------> <BOL>⏏<I EOL>
    expecting any of:

I wouldn’t recommend this, though, because I can choose many other delimiters:

> Q🐱I have some >> inside🐱
I have some >> inside

I can’t use anything I like as a delimiter, though, and some of the exclusions may surprise you:

Prohibited Reason
# start of a comment
( Looks like a function call
: Starts an adverb
' Identifier character
- Identifier character
word characters Because that’s crazy

The rest of the quoting mechanisms build up from there. Other quoting forms have their own operators, such as q// for single quoting with limited escaping and qq// for double quoting with interpolation. The Q can handle them will an adverb:

q/This has a backslash \n/
Q:q//This has a backslash \n/
qq/This and a newline \n/
Q:qq/This and a newline \n/


  1. You actually can use `’`, `-` and `()` for quoting:
    q ‘message’ or q (message)

    But you must have a space between them for identifiers. You still can’t use `q :message:`

    1. By “can”, do you mean the current implementation allows it or the spec allows it? The latest docs say that identifier characters are disallowed. In most cases I’m avoiding uses that the docs don’t allow because I figure the interpreter will change.

      1. See this here. https://docs.perl6.org/language/quoting#Literal_strings:_Q
        “Some delimiters are not allowed *immediately* after Q, q, or qq.”

        And see the example. I was the one who added this to the docs and I probably wouldn’t have realized this if not for testing 15+ delimiters for my Atom Perl6 syntax highlighter and verifying each one was usable by Perl 6.

        The synopsis doesn’t specify that identifiers are not allowed, but it is not workable to have identifiers in there without a space.

        1. For what it’s worth, your patch showed up after I’d already written this article. If I had more than my phone this week I could do major surgery. 🙂

          And, another commenter points out that the # character doesn’t work as a delimiter either.

Leave a Reply

Your email address will not be published. Required fields are marked *