Course of Raku / Advanced / More about built-in types / Quoting constructs 🆕 / Exercises / A closure in a q string
Solution: A closure in a q string
Here is a possible solution to the task.
Code
my $x = 10;
say q:c/$x squared is {$x ** 2}/;🦋 You can find the source code in the file closure-quote.raku.
Output
$x squared is 100Comments
The
:c(closure) adverb switches on interpolation of embedded{ … }code in the otherwise-literalqform. Inside the braces,$xis real code, so{$x ** 2}evaluates to100.The
$xoutside the braces is left exactly as written, because the scalar adverb:sis not enabled. This is the whole point of the per-feature adverbs: you get embedded code without also turning on$-interpolation.Adverbs can be stacked. Add
:sas well, and the leading$xis interpolated too — both features are now on at once:
my $x = 10;
say q:c:s/$x squared is {$x ** 2}/; # 10 squared is 100