use strict is really a bundle of three different behaviours: strict refs, strict subs, and strict vars. You can test for each individually, but only at compile time. (The behaviour of strict subs and strict vars happens entirely at compile time anyway, so there's probably little practical reason why you'd want to test for them at run time.)
The way you do it is to peek at the built in variable called $^H which is an integer consisting of various bit flags. Bit 0x0002 tells you whether strict refs are enabled; bit 0x0200, strict vars; and bit 0x0400, strict subs.
It's also possible in a sub call to peek at your caller's copy of $^H - see caller.
Here's a quick example:
use v5.10;
use strict;
BEGIN {
say "1";
say "strict refs are enabled" if $^H & 0x0002;
say "strict subs are enabled" if $^H & 0x0200;
say "strict vars are enabled" if $^H & 0x0400;
};
no strict 'refs';
BEGIN {
say "2";
say "strict refs are enabled" if $^H & 0x0002;
say "strict subs are enabled" if $^H & 0x0200;
say "strict vars are enabled" if $^H & 0x0400;
};
no strict;
use strict 'subs';
BEGIN {
say "3";
say "strict refs are enabled" if $^H & 0x0002;
say "strict subs are enabled" if $^H & 0x0200;
say "strict vars are enabled" if $^H & 0x0400;
};
That having been said, the scoping rules for pragmata are pretty easy. A pragma (such as strict or warnings) takes effect starting at where it is declared, and continuing to either:
- the end of the file; or
- the end of the scope (i.e. an area delimited by curly brackets { ... }, or a stringy eval) it was declared in
... whichever comes sooner.
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
|