q//#
Single-quoted string literal with a choice of delimiter.
q// is the generic form of the customary '...' string literal. It
produces a compile-time constant string, does not interpolate
variables, and does not process the backslash escape sequences
that qq// and regex literals recognise. Reach for q// whenever
the literal you want to write would be awkward under '...' — most
often because the string itself contains single quotes — or when a
different delimiter simply reads better in context.
Synopsis#
q/STRING/
q(STRING)
q[STRING]
q{STRING}
q<STRING>
q!STRING!
q#STRING#
'STRING'
Any non-alphanumeric character works as a delimiter. The four ASCII
bracket pairs ((), [], {}, <>) nest; all other delimiters do
not. '...' is a shorthand for q(...) with identical semantics.
What you get back#
A string in scalar context — the exact byte sequence between the opening and closing delimiter, with two rewrites:
\\becomes a single backslash.\<delim>becomes a literal copy of the delimiter character (this is what lets you include the delimiter inside the body).
Every other character, including every other \, stays verbatim.
There is no \n, no \t, no $var expansion, no \x{...}, no
\N{...} — the inside of q// is as close to “what you typed is
what you get” as Perl offers.
In list context q// still evaluates to the single string.
Delimiters and nesting#
Non-bracketing delimiters use the same character fore and aft:
my $a = q!I said, "You said, 'She said it.'"!;
my $b = q/path/to/file/; # WRONG — ends at the second "/"
my $b = q{path/to/file}; # RIGHT — braces nest, slashes are literal
Bracket delimiters nest, so balanced brackets inside the body are fine and only an unbalanced closer ends the string:
my $c = q{foo{bar}baz}; # "foo{bar}baz"
my $d = q(f(o(o)b)ar); # "f(o(o)b)ar"
Whitespace between q and the delimiter is allowed except when
the delimiter is # — q#foo# is the string "foo", but q #foo#
is the operator q followed by a line comment, and the string body
continues on the next line. When the delimiter is a word character
(matches /\w/), whitespace between q and the delimiter is
required: q XfooX is "foo", qXfooX is a syntax error.
Escape rules#
Inside q// the backslash has exactly two special jobs:
Sequence |
Produces |
|---|---|
|
a single backslash |
|
a literal copy of the delimiter |
Anything else beginning with \ stays as two characters:
print q(\n), "\n"; # prints "\n" then a real newline
print length q(\n); # 2
print q(\t\r\0); # prints "\t\r\0" — six characters
This is the single most frequent source of confusion for authors
coming from double-quoted strings — in q// and '...', \n is a
backslash followed by the letter n, not a newline.
Examples#
Embedding both quote marks without escaping:
my $msg = q{I said "hello", she said 'hi'.};
Windows-style path, where backslashes are plentiful and double-quote interpolation would be a hazard:
my $path = q(C:\Users\alice\Documents);
print $path; # C:\Users\alice\Documents
A literal $ without interpolation — qq// would require escaping
every $ and @, q// leaves them alone:
my $expr = q($total = $x + $y);
print $expr; # $total = $x + $y
The one-and-only way to put the delimiter inside the body: escape it with a backslash. Note that the backslash itself is consumed.
my $s = q!He said "ouch\!" loudly!;
print $s; # He said "ouch!" loudly
A regex pattern stored as a plain string, deferred to qr// at use
site — q// keeps backslashes literal so the pattern survives intact:
my $pat = q(\d{3}-\d{4});
if ($phone =~ /$pat/) { ... }
Multi-line literal with no escape processing — each line break in the source becomes a line break in the string:
my $sql = q{
SELECT id, name
FROM users
WHERE active = 1
};
Edge cases#
q//is compile-time. The parser resolves the string when the source is read; there is no runtime cost beyond using the already- built constant. Same as'...'.'...'andq(...)are indistinguishable after parsing. The choice is purely stylistic — pick whichever reads better for the content.Empty string.
q()and''both yield the zero-length string.No
\n, no\t, no interpolation, ever. If you need either, switch toqq//or"...". Mixing the two in one line is fine:q(literal ) . qq(interpolated $x).Backslash at end of body. A single
\immediately before the closing delimiter escapes it, soq(foo\)is a syntax error — the\)becomes a literal)and the string is unterminated. Writeq(foo\\)to end a string with one backslash.Unbalanced brackets with bracket delimiters. Because brackets nest,
q{foo{bar}is a syntax error — the inner{opens a nested level and the outer}closes it, leaving the string unterminated. Use a non-bracket delimiter, or balance the brackets, or escape one of them:q{foo\{bar}.#as delimiter needs no space fromq, but every other delimiter that is a word character (letters, digits,_) requires whitespace betweenqand the opening delimiter.Quoting Perl code is fragile.
q{ if ($x eq "}") { ... } }is a syntax error: the}inside the embedded double-quoted string closes the outer brace before Perl sees the inner string. Tools likeText::Balancedexist because hand-writing a robust Perl source quoter is unpleasant.Parser ambiguity with
/as delimiter.q/.../uses/the same waym//does. Inside the body no regex syntax applies, but a slash in the content will end the string prematurely — pick a different delimiter (q{...},q(...)) when slashes appear in the text.Heredocs with
<<'TAG'follow the same escape rules asq//: no interpolation, no escape processing beyond\\and\TAGnot being special (the tag terminates on its own line). Use a single-quoted heredoc when you wantq//semantics across many lines without a closing delimiter in the middle of the text.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
qq//— double-quoted literal; same delimiter flexibility asq//plus variable interpolation and the full\n/\t/\x{...}escape vocabularyqw//— list of whitespace-separated words; what you want when the literal is a list of short tokens, not a single stringqr//— compile a regex literal; the pattern-matching member of the quote-like familyqx//— run a command in the shell and capture its output; same delimiter rules, semantics of backticksquotemeta— runtime equivalent of\Q...\E, useful when you need to feed aq//literal into a regex as a fixed string$"— list separator used when arrays interpolate insideqq//; irrelevant toq//itself but the usual reason someone reaches forq//is to avoid this machinery