Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: next in continue doesn't DWIM

by Eily (Deacon)
on Nov 14, 2013 at 12:13 UTC ( #1062560=note: print w/ replies, xml ) Need Help??


in reply to next in continue doesn't DWIM

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.


Comment on Re: next in continue doesn't DWIM
Download Code
Re^2: next in continue doesn't DWIM
by QM (Vicar) on Nov 14, 2013 at 14:29 UTC
    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 }

        I agree with you about changing entrenched behaviors, and POLA. Oh, well.

        I applaud your examples of almost justifiable code. However, I think that these should be refactored to meet POLA as well. IMNSHO, the point of the continue block is to have a landing place for cleanup before restarting the loop, where, for convenience, any number of nexts can expect to go to get the house in order before the next loop. (last and redo don't skip any continue block.)

        In the first example, if that code really needs to be in continue for some reason (I realized that process($data); might just be a placeholder for something bigger), then I would rewrite the continue block like so:

        continue { do { $data = getSample(); print $logfile "Input contained $data"; } until (not invalid($data)); }

        But I would also argue, for serious code, that even this doesn't belong here, and the main loop body needs to be refactored. I guess I'm thinking of a new aphorism, something like:

        Don't do anything significant in a continue.

        I would approach the second example much the same way, so I leave the details to OMAR.

        Anyway, thanks for the debate. Short of getting anything changed, at least I've got it out of my system, even if I end up needing a Ka D'argo-like beating to do so =)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1062560]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-12-18 21:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (66 votes), past polls