Course of Raku / Advanced / Control flow / Statement prefixes 🆕 / Exercises / The value of a do block
Solution: The value of a do block
Here is a possible solution to the task.
Code
my $label = do if 7 > 5 { 'big' } else { 'small' };
say $label;🦋 You can find the source code in the file do-block.raku.
Output
bigComments
A bare
ifis a statement and has no usable value. Thedoprefix turns the wholeif/elseinto an expression.Since
7 > 5is true, the expression yields'big', which is assigned to$label. The same trick works withdo givenanddo for.The
{ }braces are not optional. Raku’sifalways takes a block, so you cannot shorten the branches toif 7 > 5 'big' else 'small'— that fails to compile with Missing block.