On Wed, Apr 28, 2004 at 11:17:43PM -0000, Yitzchak Scott-Thoennes wrote: > exiting an eval BLOCK via last when there is no loop block there > sends control to the wrong place: > > This is correct: > > $ perl -we'{eval { print "1"; last; print "2" }; print "3"} print "4";' > Exiting eval via last at -e line 1. > 14 [snip] > This is also correct: > > $ perl -we'{eval { print "1"; last; print "2" }; print "3"} print "4";' > Exiting eval via last at -e line 1. > 14 They both look identical to me? > The following is just plain weird. Once it's printed "3", why is it then > "Exiting subroutine via last"? It should give both warnings plus the > Can't "last" outside a loop block warning, and only print "1": > > $ perl -we'sub foo { eval { print "1"; last; print "2" }; print "3"} foo; > print "4";' > Exiting eval via last at -e line 1. > Exiting subroutine via last at -e line 1. > 134 The eval { } is trapping the fatal error C<Can't "last" outside a loop block> Although frankly I think that last-ing out of a sub should be a fatal error too rather than just a warning; otherwise you get the following spooky action at a distance: $ ./perl -wle' sub f { redo if $_[0] > 2 } for (1..5) { print $_; f($_) }' 1 2 3 Exiting subroutine via redo at -e line 1. 3 Exiting subroutine via redo at -e line 1. 3 Exiting subroutine via redo at -e line 1. 3 ... ie you can control the loop that calls the sub from within the sub! -- Spock (or Data) is fired from his high-ranking position for not being able to understand the most basic nuances of about one in three sentences that anyone says to him. -- Things That Never Happen in "Star Trek" #19Thread Previous | Thread Next