Perl6-Doc
view release on metacpan or search on metacpan
share/Synopsis/S02-bits.pod view on Meta::CPAN
to be part of the string outside of the interpolation, and subject
to the normal backslashing rules of that quote context:
my $a = 42;
"Val = $a\[junk\]"; # Val = 42[junk]
"Val = $a\[junk]"; # Val = 42[junk]
"Val = $a\ [junk]"; # Val = 42 [junk]
"Val = $a\.[junk]"; # Val = 42.[junk]
=item *
In order to interpolate an entire array, it's necessary now to subscript
with empty brackets:
print "The answers are @foo[]\n"
Note that this fixes the spurious "C<@>" problem in double-quoted email addresses.
As with Perl 5 array interpolation, the elements are separated by a space.
(Except that a space is not added if the element already ends in some kind
of whitespace. In particular, a list of pairs will interpolate with a
tab between the key and value, and a newline after the pair.)
=item *
In order to interpolate an entire hash, it's necessary to subscript
with empty braces or angles:
print "The associations are:\n%bar{}"
print "The associations are:\n%bar<>"
Note that this avoids the spurious "C<%>" problem in double-quoted printf formats.
By default, keys and values are separated by tab characters, and pairs
are terminated by newlines. (This is almost never what you want, but
if you want something polished, you can be more specific.)
=item *
In order to interpolate the result of a sub call, it's necessary to include
both the sigil and parentheses:
print "The results are &baz().\n"
The function is called in item context. (If it returns a list anyway,
that list is interpolated as if it were an array in string context.)
=item *
In order to interpolate the result of a method call without arguments,
it's necessary to include parentheses or extend the call with something
ending in brackets:
print "The attribute is $obj.attr().\n"
print "The attribute is $obj.attr<Jan>.\n"
The method is called in item context. (If it returns a list,
that list is interpolated as if it were an array.)
It is allowed to have a cascade of argumentless methods as long as
the last one ends with parens:
print "The attribute is %obj.keys.sort.reverse().\n"
(The cascade is basically counted as a single method call for the
end-bracket rule.)
=item *
Multiple dereferencers may be stacked as long as each one ends in
some kind of bracket or is a bare method:
print "The attribute is @baz[3](1, 2, 3).gethash.{$xyz}<blurfl>.attr().\n"
Note that the final period above is not taken as part of the expression since
it doesn't introduce a bracketed dereferencer. The parens are not required
on the C<.gethash>, but they are required on the C<.attr()>, since that
terminates the entire interpolation.
In no case may any of the top-level components be separated by
whitespace or unspace. (These are allowed, though, inside any
bracketing constructs, such as in the C<(1, 2, 3)> above.)
=item *
A bare closure also interpolates in double-quotish context. It may
not be followed by any dereferencers, since you can always put them
inside the closure. The expression inside is evaluated in string item
context. You can force list context on the expression using
the C<list> operator if necessary. A closure in a string establishes
its own lexical scope. (Expressions that sneak in without curlies,
such as C<$(...)>, do not establish their own lexical scope, but use
the outer scope, and may even declare variables in the outer scope, since
all the code inside (that isn't in an eval) is seen at compile time.)
The following means the same as the previous example.
print "The attribute is { @baz[3](1,2,3).gethash.{$xyz}<blurfl>.attr }.\n"
The final parens are unnecessary since we're providing "real" code in
the curlies. If you need to have double quotes that don't interpolate
curlies, you can explicitly remove the capability:
qq:c(0) "Here are { $two uninterpolated } curlies";
or equivalently:
qq:!c "Here are { $two uninterpolated } curlies";
Alternately, you can build up capabilities from single quote to tell
it exactly what you I<do> want to interpolate:
q:s 'Here are { $two uninterpolated } curlies';
=item *
Secondary sigils (twigils) have no influence over whether the primary sigil
interpolates. That is, if C<$a> interpolates, so do C<$^a>, C<$*a>,
C<$=a>, C<$?a>, C<$.a>, etc. It only depends on the C<$>.
=item *
No other expressions interpolate. Use curlies.
=item *
( run in 1.501 second using v1.01-cache-2.11-cpan-39bf76dae61 )