QM has asked for the wisdom of the Perl Monks concerning the following question:
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 (Monsignor) on Nov 14, 2013 at 12:13 UTC | |
by QM (Parson) on Nov 14, 2013 at 14:29 UTC | |
by Eily (Monsignor) on Nov 20, 2013 at 18:23 UTC | |
by QM (Parson) on Nov 21, 2013 at 09:33 UTC | |
by Eily (Monsignor) on Nov 21, 2013 at 10:42 UTC | |
|
Re: next in continue doesn't DWIM
by hdb (Monsignor) on Nov 14, 2013 at 12:04 UTC | |
by Eily (Monsignor) on Nov 14, 2013 at 12:15 UTC | |
by hdb (Monsignor) on Nov 14, 2013 at 12:19 UTC |