Phasers
ENTER / LEAVE & FIRST / LAST
FullBlock-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
leaveFIRST 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
lastNotes #
LEAVEis guaranteed to run however the block is exited, so it is the structured way to release resources — the equivalent oftry/finallyelsewhere.- The full loop set is
FIRST,NEXT(after each iteration), andLAST;NEXTis handy for per-iteration bookkeeping. - Phasers see the lexical scope they are written in, so they can read and update the surrounding block's variables.