SPUG: Using last in non-loop blocks
El JoPe Magnifico
ext-spug at jope.net
Thu Apr 22 22:01:47 CDT 2004
Related question: Will this limitation still apply in Perl6?
The foreword to Apocalypse 4 says the following:
[...] control flow must be able to leak out of blocks in a
controlled fashion. Obviously, falling off the end of a block
is the most "normal" way, but we need to exit blocks in other
"abnormal" ways as well. Perl 5 has several different ways of
exiting a block: return, next, last, redo, and die, for instance.
The problem is that these various keywords are hard-wired to
transfer control outward to a particular built-in construct,
such as a subroutine definition, a loop, or an eval. That works
against our unifying concept that every block is a closure.
In Perl 6, all these abnormal means of block exit are unified
under the concept of exceptions. A return is a funny kind of
exception that is trapped by a sub block. A next is an exception
that is trapped by a loop block.
And later in the same doc, in the RFC 199 discussion:
The interesting policy question as we go on will be whether a
given construct responds to a given exception or not. Some
exceptions will have to be restricted in their use. For instance,
we should probably say that only explicit sub declarations may
respond to a return.
I saw no hints as to a decision on whether "last" would be allowed
inside a non-loop block, though the general mood in decisions so far
seems to be toward permissiveness unless there is a convincing reason
to the contrary. And I can't think of one in this case.
On Thu, 22 Apr 2004 ced at carios2.ca.boeing.com wrote:
> diagnostics provides an explanation:
> Can't "last" outside a loop block at ./test1.pl line 6 (#1)
> (F) A "last" statement was executed to break out of the current block,
> except that there's this itty bitty problem called there isn't a current
> block. Note that an "if" or "else" block doesn't count as a "loopish"
> block, as doesn't a block given to sort(), map() or grep().
More information about the spug-list