Phasers

At compile time, as soon as it's parsed At compile time, after compilation (reverse order) At run time, before the main program

At run time, at program exit (reverse order) Every time a block is entered

Every time a block is left (any reason, including exceptions) When a block is left successfully

When a block is left unsuccessfully (exception or undefined/Failure return) On the first loop iteration

At the end of each loop iteration (before the next) After the last loop iteration Assertion checked before the block runs Assertion checked after the block runs

Control exception handler (warn, next, last, take, emit, done) When a Supply block's tap is closed

When a Supply (in react/supply) reports an error Phasers that run only with --doc

BEGIN { say "1. compile time" }
INIT  { say "2. before run" }
END   { say "5. at exit" }
say "3. main program";

for 1..3 {
    FIRST { say "starting loop" }
    NEXT  { say "  finished iteration $_" }
    LAST  { say "loop done" }
    say "  processing $_";
}

sub transfer($amount) {
    PRE  { $amount > 0 }                  # design-by-contract precondition
    POST { $_ ~~ Numeric }                # postcondition: $_ is the return value
    ENTER { say "entering transfer" }
    LEAVE { say "leaving transfer" }
    KEEP  { say "transfer succeeded" }
    UNDO  { say "transfer failed, rolling back" }
    $amount * 1.0
}
transfer(100);
say "4. done";

# Phasers return values: my $start = ENTER now;
sub timed { my $t = ENTER now; LEAVE say "took {now - $t}s"; sleep 0.01 }
timed();