die $@ if $@
Be careful with that, see Bug in eval in pre-5.14 - the better pattern is eval { ...; 1 } or do { handle_error() };
Hypothetically, the "early return" case can be handled by wrapping early-returnable logic in a separate function, though I can see that this may be cumbersome in some contexts.
There are several different ways to handle a kind of "return early but perform cleanup" in Perl, which one is most applicable depends on the situation (e.g. simple control flow, as you said using a separate function, the code shown in this thread, finally from a module like Try::Tiny, End, ...). If I were to use the bare block with last method, personally I'd prefer to label the block to make it absolutely clear where the last will jump to, and make it immune to accidentally adding in another bare block:
{ print "A\n"; FOO: { print "B\n"; { last FOO; print "C\n"; } print "D\n"; } # last jumps here print "E\n"; }
In reply to Re^5: 'finally' block in Perl?
by haukex
in thread 'finally' block in Perl?
by John M. Dlugosz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |