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

big

Comments

  1. A bare if is a statement and has no usable value. The do prefix turns the whole if/else into an expression.

  2. Since 7 > 5 is true, the expression yields 'big', which is assigned to $label. The same trick works with do given and do for.

  3. The { } braces are not optional. Raku’s if always takes a block, so you cannot shorten the branches to if 7 > 5 'big' else 'small' — that fails to compile with Missing block.

Course navigation

The value of a do block   |   Force eager evaluation