Course of Raku / Advanced / Control flow / Block-related phasers / Exercises / Enter a block

Solution: Enter a block

Here is a possible solution to the task.

Code

for 1..2 {
    ENTER say '--entering';
    say "body $_";
}

🦋 You can find the source code in the file enter-a-block.raku.

Output

--entering
body 1
--entering
body 2

Comments

  1. ENTER runs each time the block is entered, just before its body. Since the loop enters its body once per iteration, --entering is printed before each body line.

  2. This is the difference between ENTER and FIRST: FIRST would run only once for the whole loop, whereas ENTER runs on every entry.

Course navigation

Enter a block   |   After each step