Course of Raku / Advanced / Control flow / Block-related phasers / Exercises / Clean up on leave
Solution: Clean up on leave
Here is a possible solution to the task.
Code
sub work {
LEAVE say 'Cleanup';
say 'Working';
return;
say 'never reached';
}
work();🦋 You can find the source code in the file clean-up-on-leave.raku.
Output
Working
CleanupComments
After
Workingis printed, thereturnexits the subroutine immediately, sonever reachedis never printed.Even though the body was left early, the
LEAVEphaser still fires on the way out, printingCleanup. This guarantee is exactly whyLEAVEis the right place to release resources — it runs no matter how the block ends.