perlquestion
LanX
Perl still surprises me, I found this code example in [mod://B::CC]<P>
<c>
sub skip_on_odd { next NUMBER if $_[0] % 2 }
NUMBER: for ($i = 0; $i < 5; $i++) {
skip_on_odd($i);
print $i;
}
</c>
produces the output <c>024</c><P>
I would never believe that it works because the goto and the label are in different scopes but well...<P>
from [doc://goto] I can read<P>
<tt>... It can be used to go almost anywhere else
within the dynamic scope, including out of subroutines,...</tt><P>
So what exactly is a "dynamic scope" here? Does it mean that the label NUMBER is kind of a localized global, which is visible in called subs? <P>
And jumping out of the sub is guarantied to clean the call stack (such avoiding any overflows)?<P>
So please explain why a normal call works, but not the out-commented goto call (which tries to avoid the superfluous call frame)?<P>
<c>
sub jump { goto NESTED };
sub cont {
for $i (0..3) {
for $j ("a".."c") {
print "$i,$j\t";
jump(); #1
#goto &jump; #2
}
NESTED:
}
}
cont();
</c><P>
OUTPUT1 <c> 0,a 1,a 2,a 3,a</c><P>
OUTPUT2 <c>
Can't find label NESTED at /home/lanx/B/PL/PM/goto.pl line 1.
0,a
</c>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-708738">
<p>Cheers Rolf
</div></div>