On Fri, Dec 19, 2008 at 01:47:08AM +0100, Aristotle Pagaltzis wrote:
: And I just realised how to best do that in Perl 5:
: 
:     goto INVARIANT;
: 
:     while ( @stuff ) {
:         $_->do_something( ++$i ) for @stuff;
: 
:         INVARIANT:
:         @stuff = grep { $_->valid } @stuff;
:     }
: 
: I am not sure why this works, to be honest. That is, I don’t know
: whether it’s an intentional or accidental feature that execution
: doesn’t merely fall off the end of the loop body after jumping
: into the middle of it, but loops back to the top, despite not
: having executed the `while` statement first.
: 
: But it does work.
: 
: And it says exactly what it’s supposed to say in the absolutely
: most straightforward manner possible. The order of execution is
: crystal clear, the intent behind the loop completely explicit.

It is specced to work correctly in Perl 6 as well.  The policy
(shared with Perl 5) is that you may use goto into any loop that
doesn't depend on an initializer.  It's illegal to go into
a for 1..10 loop, for instance, since the loop won't be initialized
correctly.

Larry

Reply via email to