qq//#
Build a double-quoted, interpolated string with a delimiter you pick.
qq// is the long form of the "..." string literal. It produces the
same string "" would, so qq/hello $name\n/ and "hello $name\n"
are interchangeable. The point of qq is the delimiter: when the
content contains " (or backslash sequences that would interfere with
double quotes), you pick a different delimiter and keep the string
readable instead of threading \" through it.
Synopsis#
qq/STRING/
qq{STRING}
qq(STRING)
qq[STRING]
qq<STRING>
qqXSTRINGX # any non-word, non-whitespace char works as delimiter
What you get back#
A scalar string. Variables referenced inside are interpolated at the
point qq// is evaluated (not at parse time); backslash escape
sequences are expanded. The result has no implicit newline — you have
to write \n yourself.
Choosing a delimiter#
Any character that is not a word character (/\w/) and not whitespace
may follow qq. The four bracket pairs nest:
qq(foo (bar) baz) # "foo (bar) baz" — inner () balanced
qq{ if ($x) { 1 } else { 0 } }
qq[a[b[c]d]e]
qq<<a<b>c>>
Non-bracketing delimiters use the same character at both ends and do not nest:
qq|one|two| # SYNTAX ERROR — first | closes
qq/a\/b/ # "a/b" — backslash escapes the delimiter
If the delimiter is a word character, whitespace between qq and the
delimiter is required:
qq XfooX # OK — "foo"
qqXfooX # WRONG — parsed as identifier qqXfooX
# as delimiter needs no space and is occasionally handy for strings
containing every other punctuation mark:
qq#He said "hi" & waved.# # "He said \"hi\" & waved."
The extra_paired_delimiters feature (5.36+) recognises a wide set
of Unicode bracket pairs as nesting delimiters; enable it with
use feature 'extra_paired_delimiters' when you want, say, guillemets
or CJK brackets.
What gets interpolated#
Scalars:
$x,$hash{key},$array[0],$ref->{k}[2].Arrays:
@arrexpands in order, elements joined by$"(default" "). Equivalent tojoin $", @arr.Slices:
@h{qw(a b)},@a[1..3].Dereferences with arrows and subscripts:
$ref->{key}[0].
What is not interpolated:
Method calls:
$obj->methinsideqq//interpolates$objand leaves the literal text->methin the string.Function calls:
@{[ func() ]}is the usual workaround — array reference dereference runs arbitrary code and splices its result in.Punctuation arrays other than
@_,@+,@-: only interpolate when braced (@{*}). Bare@*is a literal@*.
my $obj = Some::Class->new;
qq/$obj->name/; # interpolates $obj, leaves "->name"
qq/@{[ $obj->name ]}/; # calls the method, interpolates result
Escape sequences#
Available inside qq// (and anywhere else interpolation happens):
Sequence |
Meaning |
|---|---|
|
tab (HT) |
|
newline (platform-virtual line terminator) |
|
carriage return (CR) |
|
form feed (FF) |
|
backspace (BS) |
|
alarm / bell (BEL) |
|
escape (ESC) |
|
NUL |
|
literal backslash |
|
literal double quote |
|
hex char, 0x00–0xFF |
|
hex char, any codepoint (braced form) |
|
octal char, any codepoint |
|
octal char, 000–777 (prefer |
|
control char — 64 xor’d with uppercased next char |
|
named Unicode char (needs |
|
Unicode char by codepoint (always Unicode, even on EBCDIC) |
Case and quoting modifiers apply until \E or end of string:
Sequence |
Meaning |
|---|---|
|
lowercase the next character only |
|
titlecase (not uppercase!) the next character only |
|
lowercase until |
|
uppercase until |
|
foldcase until |
|
quote regex metacharacters until |
|
end the most recent |
\L, \U, \F, \Q stack — each needs its own \E.
say "\u$name"; # capitalise first letter
say "\U$name\E's account"; # uppercase $name, rest untouched
say "match $file literally: \Q$file\E";
\Q inside qq// is applied after interpolation, so
qq/\Q$pat\E/ equals quotemeta($pat). (In regex quote operators
qr// and m//, \Q is applied after interpolation but before
regex compilation — the difference matters when $pat itself
contains regex metacharacters you want to keep literal.)
Perl has no \v escape in string literals — it is a regex
metacharacter. Use \x0b, \cK, or \N{VT} for a vertical tab.
Global state it touches#
$"— list separator used when interpolating arrays and slices. Default" ". Change withlocal $" = ","for one scope.
No other interpreter globals participate in qq// evaluation itself.
Interpolated variables are of course whatever their current values
are at the point qq// runs.
Examples#
Plain interpolation, identical in meaning to the "..." form:
my $name = "world";
my $s = qq/hello, $name!\n/; # "hello, world!\n"
String that contains double quotes — qq with a different delimiter
avoids backslash litter:
my $msg = qq{He said "yes" and then "maybe".};
# vs: "He said \"yes\" and then \"maybe\"."
Multi-line HTML snippet with nested braces — the () pair nests:
my $user = "rj";
my $html = qq(
<p>Welcome, $user.</p>
<p>You have @{[ scalar @inbox ]} messages.</p>
);
Array interpolation with a custom separator:
my @tags = qw(perl rust docs);
local $" = ", ";
say qq/tags: @tags/; # "tags: perl, rust, docs"
Method-call trap — -> is literal inside qq//:
my $obj = File::Spec->new;
qq/path is $obj->rel2abs('.')/; # "path is <stringified $obj>->rel2abs('.')"
qq/path is @{[ $obj->rel2abs('.') ]}/;
# actually calls rel2abs()
Building an SQL fragment where $ident is untrusted — \Q..\E keeps
embedded %, _, and backslashes literal under a LIKE clause only
after you have handled proper parameterisation; for actual SQL
escaping use the driver’s quoting, not \Q:
my $needle = "100% sure";
my $pat = qq/\Q$needle\E/; # the regex-safe form
# $pat eq "100\\%\\ sure"
Stacked modifiers:
say qq/\Qfoo \ubar \Ubaz\E qux\E done/;
# "foo\ Bar\ BAZ qux done"
# \Q quotes spaces/?; \u titlecases b; \U uppercases baz until \E;
# outer \E ends \Q.
Edge cases#
Parse-time delimiter choice. Perl picks the closing delimiter the moment it reads the opening one.
qq/.../expects a/; there is no way to switch mid-string. If you need both kinds of braces, pick a non-bracket delimiter.Backslash at end of string with non-bracket delimiter.
qq/a\/isa/(backslash escapes the delimiter, string continues); to get a literal trailing backslash, change delimiter:qq{a\\}.No nesting for non-bracket delimiters.
qq|a|b|is the stringafollowed by the identifierbfollowed by|— a syntax error in most contexts. Pick bracket delimiters when the content contains the delimiter char.#as delimiter is a special case only because of how comments interact:qq#...#is a string, butqq #...#(with whitespace) is the operatorqqfollowed by a comment, with the string taken from the next line. This mirrors the behaviour of allq-family operators.Empty string.
qq//is the empty string, same as"".Interpolated
undef. Stringifies to the empty string and, underuse warnings, triggers anuninitializedwarning at the point of interpolation, not at the point$varwas assignedundef.$or@followed by non-identifier. Stays literal:qq/cost: \$5/andqq/cost: $5/both producecost: $5(there is no variable named$5in the Perl 5 identifier sense —$5is a regex capture, which does exist, so the second form interpolates capture group 5, whatever it currently is). For a literal$or@, escape it:\$,\@.\N{...}withoutcharnames. Named characters requireuse charnames ':full'(or similar) to be loaded in scope; without it you get a compile-time error.\N{U+HEX}and the single-name forms in the:looseset work without it.Locale-sensitive case mapping. With
use localeincludingLC_CTYPE,\l,\u,\L,\Uuse the current locale’s case tables. Without it, Unicode case tables are used for codepoints above 0xFF, and ASCII rules below.\Fuses Unicode foldcase regardless of locale (except under a UTF-8 locale, where it matches\L).One level only. The result of interpolation is not re-scanned.
my $v = '$x'; qq/$v/is the four-character string$x, not the value of$x.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
q//— single-quoted literal, no interpolation, same delimiter rulesqw//— word-list literal, produces a list of strings split on whitespaceqx//— interpolated string executed as a shell command, captures stdoutqr//— interpolated regex, compiled once and reusablesprintf— reach for this when the work is formatting numbers or padding fields, not splicing variables into textquotemeta— the function\Q..\Ecallsjoin— what array interpolation is secretly doing, with$"as the separator$"— list separator used when an array interpolates into a double-quoted stringQuote and Quote-like Operators — the full table of
q,qq,qx,qw,qr,m,s,trand their shared delimiter and escape rules