The only problem with it is that loop labels are lexically scoped, not dynamically, so you can never make sure that your loop labels don't accidentally clash with those of a function you call from another module.
Huh? Looks dynamic to me:
#!/usr/bin/env perl
use strict;
SOME_LOOP:
for my $outer (1..2) {
print "outer: before callback with $outer\n";
call_my_callback( sub { next SOME_LOOP } ); # you say this should ad
+vance "$outer";
print "outer: after callback with $outer\n";
}
sub call_my_callback {
SOME_LOOP:
for my $inner (1..2) {
print "inner: before callback with $inner\n";
$_[0]->(); # "neeeexxxt!"
print "inner: after callback with $inner\n";
}
}
This runs (as I expected), producing:
outer: before callback with 1
inner: before callback with 1
inner: before callback with 2
outer: after callback with 1
outer: before callback with 2
inner: before callback with 1
inner: before callback with 2
outer: after callback with 2
Note that "next" is seeing the inner loop (dynamic, like local variables), not the outer loop (lexical, like my variables). If it had ignored the inner loop (which you can simulate by changing the subroutine loop label to something else), the output would have been:
outer: before callback with 1
inner: before callback with 1
outer: before callback with 2
inner: before callback with 1
The way this works is that last/next/redo just pop contexts off the stack until they get to one that has the right name. If they go all the way to the outer program, it's an error. It's a very simple scoping rule, and very predictable, and actually is doing The Right Thing here.
|