Don't ask to ask, just ask | |
PerlMonks |
Re:while modifier( and scoping)by demerphq (Chancellor) |
on Sep 22, 2001 at 20:19 UTC ( [id://114069]=note: print w/replies, xml ) | Need Help?? |
Hi Dragonchild, actually this isn't quite right Yes, it is a statement modifier, but it's also a looping construct with its own scope. In fact modifiers of all kinds do not have their own scope. They execute in the scope of their enclosing block. This can be seen by my $x=1 if $y;$x++; where usage of the the $x after the if does not raise an exception. The reason it seems like they do when you use the do BLOCK pseudo-function is because in this case the block itself has its own scope. This is illustrated by the code below where the only variable usage that throws use strict; is the one declared inside of a do block. Another interesting point with the do usage is that the block gets executed an extra time, ie before the first evaluation of the conditional. This can be seen by the output line Do's pre-evaluation:DO{}while The compound statement forms of the modifiers are different in that they have their own scopes and scoping rules (for instance the for my $i (0..10) {} where the my's scope is actually the contents of the block. Notice that in the non do {} simple statement form, even when the condition is 0 and one might expect Perl to optimize it away the variable is created in the outside scope.
Yves
In Section
Seekers of Perl Wisdom
|
|