Course of Raku / Advanced / More about built-in types / Quoting constructs 🆕 / Exercises / A heredoc
Solution: A heredoc
Here is a possible solution to the task.
Code
my $name = 'Anna';
my $item = 'Raku Book';
my $price = 25;
my $count = 3;
print qq:to/END/;
Dear $name,
You ordered $count copies of "$item".
That comes to {$count * $price} dollars.
Thank you!
END🦋 You can find the source code in the file a-heredoc.raku.
Output
Dear Anna,
You ordered 3 copies of "Raku Book".
That comes to 75 dollars.
Thank you!Comments
The heredoc starts with
qq:torather thanq:to, so it interpolates. Like aqqstring, it fills in scalars —$name,$count,$item— and runs embedded code: the block{$count * $price}computes3 * 25, so the total75appears inline.The double quotes around
"$item"are just literal characters here; inside a heredoc there is no delimiter to escape, so they print as written while$itemstill interpolates.The body and the closing
ENDare indented by the same four spaces. The indentation of the terminator is stripped from every line, so those four spaces never reach the string — the output starts at the left margin.The heredoc already ends with a newline, so
printis used rather thansayto avoid adding a second blank line.
Course navigation
← A heredoc | Date and time →