Because use strict; and no strict 'refs'; are compile-time directives, you can't easily check for them during run-time. One way to find out if a certain scope has strict bits set, is to save $^H to a variable in a BEGIN block:
#!/usr/bin/perl
STRICT: {
use strict;
my $strict;
BEGIN { $strict = $^H }
strict_bits($strict);
}
NO_STRICT: {
no strict;
my $strict;
BEGIN { $strict = $^H }
strict_bits($strict);
}
sub strict_bits {
my $h = shift;
# from strict.pm
my %bitmask = (
refs => 0x00000002,
subs => 0x00000200,
vars => 0x00000400
);
printf "\$^H = $h = %b\n", $h;
print "strict '$_' is ",
(($h & $bitmask{$_})
? 'on'
: 'off'
),
"\n"
for sort keys %bitmask;
}
__END__
$^H = 1794 = 11100000010
strict 'refs' is on
strict 'subs' is on
strict 'vars' is on
$^H = 256 = 100000000
strict 'refs' is off
strict 'subs' is off
strict 'vars' is off
The same idea works for warnings and ${^WARN_BITS}.
My naive understanding is that $^H is attached to a scope (like the "STRICT" block above) during compile-time, in order to make the strict settings work. Apparently you can't access that setting directly during run-time.
Someone with greater B::Deparse-fu will no doubt be able to explain this in more depth. |