note
Animator
<p>Here are some others thoughts:</p>
<p>In your current implementation you define the break function in the caller's package, but what if there is already a break function in it?</p>
<p>Wouldn't it be better to allow a user-defined name, and/or return a blessed version of the coderef and make it possible to call the break method on it? (in order to this to work properly you will need to pass the object itself to the code-ref of the eval)</p>
<p>And shouldn't the break function stop the execution of the code? (although this might be rather complicated..., as in adding 'last' to the break sub won't work if the code has it's loop, using die/eval might work but that would be slower...)</p>
<p>And what about implementing next, redo and last?</p>
<p>Where next could mean, jump to the next case statement (that is the fallthru one, possibile with or without ignoring the fallthru variable (if break doesn't return immediatly))</p>
<p>Redo could mean redo the case-coderef. (Updated (old text: <strike>entire switch statement, this might be useful when a certain case code-ref modifies the input and wants to re-check it...</strike>)</p>
<p>And last could be an alias to break...</p>
<p>By creating your own loop you can figure out which one was called... Some code to demonstrate:
<code>
my ($redo, $next, $last)=(0, 0, 1);
{
my $first = 0;
for (0, 1) {
if (not $_ and $first++) { $redo=1; $last = 0; last; }
if ($_) { $next = 1; $last = 0; last; }
main_code();
$last = 0;
last;
}
}
$,=" & ";
print ($next, $redo, $last);
sub main_code { redo; } # replace redo with next and/or last to test
</code></p>
456132
456132