develooper Front page | perl.perl5.porters | Postings from May 2004

Re: [perl #29238] exiting eval via last

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
May 3, 2004 08:18
Subject:
Re: [perl #29238] exiting eval via last
Message ID:
20040503152304.GD1895@iabyn.com
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" #19

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About