quote qq

Documentation for quote qq assembled from the following types:

language documentation Quoting constructs

From Quoting constructs

(Quoting constructs) quote qq

my $color = 'blue';
say "My favorite color is $color!"

My favorite color is blue!

The qq form – usually written using double quotes – allows for interpolation of backslash sequences and variables, i.e., variables can be written within the string so that the content of the variable is inserted into the string. It is also possible to escape variables within a qq-quoted string:

say "The \$color variable contains the value '$color'";

The $color variable contains the value 'blue'

Another feature of qq is the ability to interpolate Perl 6 code from within the string, using curly braces:

my ($x, $y, $z) = 4, 3.5, 3;
say "This room is $x m by $y m by $z m.";
say "Therefore its volume should be { $x * $y * $z } m³!";

This room is 4 m by 3.5 m by 3 m.
Therefore its volume should be 42 m³!

By default, only variables with the $ sigil are interpolated normally. This way, when you write "documentation@perl6.org", you aren't interpolating the @perl6 variable. If that's what you want to do, append a [] to the variable name:

my @neighbors = "Felix", "Danielle", "Lucinda";
say "@neighbors[] and I try our best to coexist peacefully."

Felix Danielle Lucinda and I try our best to coexist peacefully.

Often a method call is more appropriate. These are allowed within qq quotes as long as they have parentheses after the call. Thus the following code will work:

say "@neighbors.join(', ') and I try our best to coexist peacefully."

Felix, Danielle, Lucinda and I try our best to coexist peacefully.

However, "@example.com" produces @example.com.

To call a subroutine use the &-sigil.

say "abc&uc("def")ghi";
# OUTPUT: «abcDEFghi␤» 

Postcircumfix operators and therefore subscripts are interpolated as well.

my %h = :1st; say "abc%h<st>ghi";
# OUTPUT: «abc1ghi␤» 

To enter unicode sequences use \x or \x[] with the hex-code of the character or a list of characters.

my $s = "I \x2665 Perl 6!";
say $s;
# OUTPUT: «I ♥ Perl 6!␤» 
$s = "I really \x[2661,2665,2764,1f495] Perl 6!";
say $s;
# OUTPUT: «I really ♡♥❤💕 Perl 6!␤» 

You can also use unicode names , named sequences and name aliases with \c[].

my $s = "Camelia \c[BROKEN HEART] my \c[HEAVY BLACK HEART]!";
say $s;
# OUTPUT: «Camelia 💔 my ❤!␤» 

Interpolation of undefined values will raise a control exception that can be caught in the current block with CONTROL.

sub niler {Nil};
my Str $a = niler;
say "alive"# this line is dead code 
CONTROL { .die };