Phasers

ENTER / LEAVE & FIRST / LAST

Full

Block-entry/exit phasers and loop first/last-iteration phasers.

Some phasers fire around blocks and loop iterations. ENTER/LEAVE run each time a block is entered and left; FIRST/LAST run on the first and last iteration of a loop.

ENTER and LEAVE #

ENTER runs on entering the block, LEAVE on leaving it — including via an exception or an early return, which makes LEAVE ideal for cleanup.

{
    ENTER say "enter";
    say "body";
    LEAVE say "leave";
}
Output
enter
body
leave

FIRST and LAST #

Inside a loop, FIRST runs only on the first iteration and LAST only on the last, while the body runs every time.

for 1..3 {
    FIRST say "first";
    say $_;
    LAST say "last";
}
Output
first
1
2
3
last

Notes #