in reply to Re: last in do vs last in while/for
in thread last in do vs last in while/for

Ok, but why works the following:

$ perl -le'do { print $i++; last if $i>5 } for (1..10); print "end"' 0 1 2 3 4 5 end

Replies are listed 'Best First'.
Re^3: last in do vs last in while/for
by ikegami (Patriarch) on Mar 04, 2009 at 20:01 UTC
    Seems that postfix for actually creates a loop. I wonder if that has always been the case.
    $ perl -MO=Concise,-exec -e'while (foo()) { bar() }' 3 <{> enterloop(next->8 last->d redo->4) v $ perl -MO=Concise,-exec -e'bar() while foo()' $ perl -MO=Concise,-exec -e'do { bar() } while foo()' $ perl -MO=Concise,-exec -e'for (foo()) { bar() }' 8 <{> enteriter(next->d last->g redo->9) lK $ perl -MO=Concise,-exec -e'bar() for foo()' 9 <{> enteriter(next->d last->g redo->a) lK

    (Output filtered using grep 'enterloop\|enteriter')

    bar() while foo() uses a goto instead. It is implemented almost identically to:

    LOOP_START: ( bar() ), goto LOOP_START if foo();

    do { bar() } while foo() is not readily representable in Perl, but it doesn't use a loop either. The closest I can get is

    LOOP_START: do { bar() }; goto LOOP_START unless foo();

    But yes, the answer is "Implementation Details". There's no real reason for it not to work except backwards compatibility.