Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How does next label work?

by PerlOnTheWay (Scribe)
on Jan 06, 2013 at 09:47 UTC ( #1011864=perlquestion: print w/ replies, xml ) Need Help??
PerlOnTheWay has asked for the wisdom of the Perl Monks concerning the following question:

X: { print "hello\n"; next X; }

I thought next is just another way to say goto,

and I was expecting the above code to go into infinite loop, but actually the block only executes once.

How does next label work actually?

Comment on How does next label work?
Download Code
Re: How does next label work?
by LanX (Canon) on Jan 06, 2013 at 09:49 UTC
    Naked blocks are documented as being internally implemented as loops which are only executed once.

    next is a loop control which reevaluates the condition, which is false now.

    It's no goto, you can only use if for lables just in front of loop-starts, OTOH gotos can jump to any lables in the same scope.

    For your purpose better try redo

    > perl { print $x++; redo if $x<6; } __END__ 012345
    or real gotos

    > perl X: { print $x++; goto X if $x<6; } __END__ 012345

    Cheers Rolf

    UPDATES:

    )from perlsyn

    Basic BLOCKs A BLOCK by itself (labeled or not) is semantically equivalent t +o a loop that executes once. Thus you can use any of the loop control statements in it to leave or restart the block.
Re: How does next label work?
by moritz (Cardinal) on Jan 06, 2013 at 10:04 UTC
    I thought next is just another way to say goto

    Well, it's not. You can see that next and goto compile to different ops internally:

    $ perl -MO=Concise -e 'X: next X' 4 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(X: main 1 -e:1) v:{ ->3 3 <"> next("X") v ->4 -e syntax OK $ perl -MO=Concise -e 'X: goto X' 4 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(X: main 1 -e:1) v:{ ->3 3 <"> goto("X") v ->4 -e syntax OK

    Another difference is this warning here:

    $ perl -wE 'sub f() { next X }; X: for (1) { f }' Exiting subroutine via next at -e line 1.

    which you don't get with goto. And of course the fact that the code above terminates, but doesn't if you replace 'next' with 'goto'.

Re: How does next label work?
by 7stud (Deacon) on Jan 06, 2013 at 10:22 UTC
    It might be helpful to think of next as jumping to just before the closing brace of the loop/block. Because your next statement is the last statement before the closing brace, it does nothing.
Re: How does next label work?
by sundialsvc4 (Monsignor) on Jan 07, 2013 at 22:42 UTC

    I’m of the opinion that this sort of thing is the very worst kind of “Perl Golf” ... a game with all water-traps and no tee.   The construct is intended to go directly to the next iteration of a “real loop” as designated by the specified label-name.   If you are caught using it where no such loop exists (in the eyes of your co-workers, not Perl), and even if it so happens that Perl’s compiler accepts it, then you nevertheless richly deserve to be designated the one who gets to clean the coffee maker for the next full year.

    (Urgh:   you do clean the coffee maker, don’t you??)

      The OP asked "How does next label work?" and provided an edge case example to emphasize his problem.

      Well that's perfectly normal ...and a well documented behavior is hardly "golf".

      And BTW Basic BLOCKs with redo at the end

      { ... ; redo if COND; }
      are a far saner way to realize do-while-loops.
      do { ... } while (COND)

      EDIT:

      Im of the opinion that especially those coworkers which can't use perldoc -f should be restricted to clean the coffee maker. ;-)

      Cheers Rolf

      I'm of the opinion that this sort of thing is the very worst kind of "Perl Golf"
      Though it's been a while since I've agreed with your opinion, I strongly agree with you this time. It is shockingly bad golf; the label in "next label" is clearly unnecessary, an outrageous waste of precious strokes.

        ... and it is also reason enough to say that one should never, ever make coffee in the office.   Always go to Starbuck’s and order a triple-shot.   Because you never know when something sentient might emerge from the unwashed office coffee-pot ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011864]
Approved by moritz
Front-paged by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-07-22 21:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (129 votes), past polls