in reply to Re^2: 'finally' block in Perl?
in thread 'finally' block in Perl?
perlsyn describes how last, redo, or next won't work (or won't do what one might expect) in if, unless, do{}, sub{}, and eval{}. last says: "Note that a block by itself is semantically identical to a loop that executes once. Thus last can be used to effect an early exit out of such a block." - a solution also described in perlsyn: "Loop control statements don't work in an if or unless, since they aren't loops. You can double the braces to make them such, though. ... This is caused by the fact that a block by itself acts as a loop that executes once".
Update: Note how in the following, the first part prints "A" and "B" (and a warning about exiting eval via last), but not "D", while the second part prints "X", "Y", and "Z".
{ print "A\n"; eval { print "B\n"; last; print "C\n"; }; print "D\n"; } { print "W\n"; eval {{ print "X\n"; last; print "Y\n"; }}; print "Z\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: 'finally' block in Perl?
by matvore (Initiate) on Jan 08, 2019 at 15:56 UTC | |
by haukex (Archbishop) on Jan 08, 2019 at 16:46 UTC |