Created
March 26, 2023 14:41
-
-
Save lucs/5a9b5c5ccfc5c7be59403e7d033ce5b4 to your computer and use it in GitHub Desktop.
Control a LEAVE block's execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I want a LEAVE block to run after a sub has completed, | |
but only if some condition is True. | |
# -------------------------------------------------------------------- | |
# Start from this. | |
sub foo1 { | |
⋯ | |
LEAVE {⋯} | |
⋯ | |
} | |
The LEAVE block runs after foo() completes, always. | |
# -------------------------------------------------------------------- | |
# Nope. | |
sub foo2 { | |
⋯ | |
if $cond { | |
LEAVE {⋯} | |
⋯ | |
} | |
The LEAVE block runs here, after the 「if」 block, but still | |
always (that is, whether $cond is True or False), because it's | |
a compile-time thing. | |
⋯ | |
} | |
# -------------------------------------------------------------------- | |
# Solution. | |
sub foo3 { | |
⋯ | |
if $cond { | |
⋯ | |
} | |
LEAVE {⋯ if $cond} | |
⋯ | |
} | |
The LEAVE block runs after foo() completes, again always, but the | |
「if $cond」 in its block prevents unwanted stuff from being run. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment