Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

next in continue doesn't DWIM

by QM (Parson)
on Nov 14, 2013 at 11:16 UTC ( #1062555=perlquestion: print w/replies, xml ) Need Help??
QM has asked for the wisdom of the Perl Monks concerning the following question:

I get bit by (next in continue) every now and then:
last, next, or redo may appear within a continue block; last and redo behave as if they had been executed within the main block. So will next, but since it will execute a continue block, it may be more entertaining.

Just curious if this is really useful, because it can lead to infinite loops that are hard to figure out. Sure, it's pretty obvious that it's an infinite loop, but it's not obvious that next is the cause:

my $try= 0; my $max_tries = 100; while ($try < $max_tries) { last if foo($try); ++$try; } continue { next if ($try % 10); print "Still running after $try tries\n"; }

As soon as ($try % 10) is true, next if ($try % 10) calls itself forever, as $try is never updated after that point. (This could be "solved" by using ($try++ % 10) instead of incrementing in the body, but this only prevents the infinite loop, and the code doesn't behave as intended.)

I would expect that a next from continue would go the the loop body, while a next from the body would go to continue.

Opinions on DWIM?

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re: next in continue doesn't DWIM
by Eily (Prior) on Nov 14, 2013 at 12:13 UTC

    Just curious if this is really useful, because it can lead to infinite loops that are hard to figure out.
    if by useful you mean used with this particular behaviour in mind (turning a continue block into a loop, not necessarily an infinite one), yeah probably. If not in clean code, maybe it is used as an obfuscation. Maybe without that precision in the doc, someone would have still expected next to mean "execute the continue block straightaway", even if this does not seem the more probable interpretation.

    You could still obtain the behaviour you were expecting though, with:

    continue {{ next if ($try % 10); print "Still running after $try tries\n"; }}
    since next will skip execution of the inner block. But it does not help readability.

      I should clarify, there are 2 issues I'm curious about:

      1. Is there is a legitimate use for this behavior.
      2. Is it possible to change the behavior in future versions, given the installed code that might depend on it (regardless of how "wrong" it might be).

      I'm sure this came about as a simple way to define next, namely, "jump to the continue block".

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        For reason of retro compatibility, and the Principle Of Least Astonishment (well, talk about DWIM ...) when people are used to this behaviour, do not expect this to change. And there might be CPAN modules that rely on this feature, which would go broken if it changed. That's actually the first thing I was told here, because I asked about a possible change of regex syntax (oh foolish me).

        Now as for a legitimate use of it ... I never saw one. But I guess it could for exemple be used to separate what should be done on all data samples and what should be only done on "valid data" like:

        $data = init(); while(still_alive) { process($data); } continue { $data = getSample(); print $logfile "Input contained $data"; next if invalid($data); }
        If process is what is important, and invalid samples are rare enough that you don't want to visually polute the main process, but you still have to deal with them this could a way to do it.

        Here is another example:

        STATE: until ($state->name eq 'Ending') { $state->action(); redo STATE if nothingHasChanged(); } continue { $state = $state->nextState(context); next STATE unless $blackList->contains($state); #here we want to go +to the next state without processing the current one }

Re: next in continue doesn't DWIM
by hdb (Monsignor) on Nov 14, 2013 at 12:04 UTC

    Even if next did what you wanted, you would run into an infinite loop if both foo($try) and $try % 10 would be true.

    Update: What I said above is not correct, see Eily's comment below.

      last's documentation states:

      The continue block, if any, is not executed:
      So a last statement always exits a loop if executed.

        You are correct, my statement is wrong, I confused myself.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1062555]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2018-07-23 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (459 votes). Check out past polls.

    Notices?