Course of Raku / Essentials / Strings
Code interpolation
The next level of interpolation is code interpolation. It allows you to have simple (and complex too!) code directly inside a double-quoted string.
The interpolated code is placed between curly braces:
my $a = 10;
my $b = 20;
say "The sum of $a and $b is {$a + $b}.";
The program prints:
$ raku t.raku
The sum of 10 and 20 is 30.
Variables again
One of the applications of this method is to help to disambiguate situations when a variable must be followed by a text that can be wrongly understood as the continuation of the variable’s name. To prevent that, use curly braces to enclose the variable:
my $how-many = 5;
my $what = 'suit';
say "There are $how-many {$what}s."; # There are 5 suits.
Without curly braces, Raku would try to interpolate a non-existent variable $whats
.
Course navigation
← Strings / Variable interpolation | Strings / Escaping special characters →
💪 Or jump directly to the exercises to this section.
Translations of this page: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська