Only lexical aka static scopes.
Eg local has a dynamic scope.
<edit>
Because of LanX answer below:
Well, local has no scope at all - it just masks a (package) global at runtime until the localizing goes out of (lexical) scope, but that's just nitpicking at wording perhaps. See also my/local, space/time (was: Re: The difference between my and local).
local has a static scope. Localized variables have a dynamic scope.
</edit>
But here you answered the question for yourself. The warning bits are compiled into each lexical scope at compile time, and a local $^W = $my_bits doesn't change them at runtime for scopes further down.
So there are two possibilities:
- localize $SIG{__WARN__} to propagate a runtime behavior
- compile and eval code to be called with a current $^W at runtime
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [d/l] [select] |
use strict;
my $lasthash = {
lastfunc => sub { last ; },
} ;
sub lastfunc {
last ;
}
sub test {
# local ($^W) = 1 ; # Activate this line to display the warnings
while(1) { lastfunc } ;
while(1) { $lasthash->{lastfunc}->(); } ;
}
test() ;
| [reply] [d/l] |
I think two variables where confused here, $^W is dynamically scoped but can hold only one boolean state, not a variety of warning bits like ${^WARNING_BITS} .
From perlvar
● $WARNING
● $^W
The current value of the warning switch, initially true if -w was used, false otherwise, but directly modifiable.
See also warnings.
Mnemonic: related to the -w switch.
● ${^WARNING_BITS}
The current set of warning checks enabled by the use warnings pragma. It has the same scoping as the $^H and %^H variables. The exact values are considered internal to the warnings pragma and may change between versions of Perl.
This variable was added in Perl v5.6.0.
I suppose that ${^WARNING_BITS} * can be dynamically changed, but then only with great care (I wouldn't be surprised if implementation changed)
The bits are supposed to be set by use warnings which is statically scoped, hence here are dragons!
*) to answer my own suspicion, ${^WARNING_BITS} are only set at compile-time and undef at run-time!
| [reply] [d/l] [select] |
| [reply] |
Dynamic scoping might be well defined, but Perl Glossary doesn't do a good job. Looking at Lexical Scoping, most people won't be any wiser.
-QM
--
Quantum Mechanics: The dreams stuff is made of
| [reply] |