Course of Raku / Advanced / Control flow / Statement prefixes 🆕
do and the value of a block
A block on its own is a statement, not an expression, so you cannot
normally assign it to a variable. The do prefix turns a
block into an expression whose value is the value of its
last statement:
my $x = do {
my $a = 3;
$a + 4;
};
say $x; # 7Inside the block you can declare variables, run several statements,
and compute a result; do hands back whatever the block
evaluates to. This is handy when producing a value needs more than a
single expression.
do also works in front of the control-flow statements,
letting them return a value too:
my $sign = do given 5 {
when * > 0 { 'positive' }
when * < 0 { 'negative' }
default { 'zero' }
};
say $sign; # positiveHere do given turns the whole
given/when into an expression that yields
positive. The same works with do if and
do for. In short, do is how you use a block,
or a control structure, in a place that expects a value.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Statement prefixes 🆕 | Quiz — do →
💪 Or jump directly to the exercises in this
section.