develooper Front page | perl.perl6.language | Postings from December 2008

Re: Support for ensuring invariants from one loop iteration to thenext?

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
December 18, 2008 16:47
Subject:
Re: Support for ensuring invariants from one loop iteration to thenext?
Message ID:
20081219004708.GA13833@klangraum.plasmasturm.org
* David Green <david.green@telus.net> [2008-12-18 19:45]:
> Well, I prefer a built-in counter like that, but I thought the
> point was that you wanted some kind of block or something that
> could be syntactically distinct?

No, that would only be a means to the end. That end is simply to
not repeat myself in writing honest code. 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.

-- 
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>

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