Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Is there a way to determine if use strict; is in effect?

by Anonymous Monk
on Feb 07, 2014 at 21:50 UTC ( [id://1073924]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way in perl to tell if use strict; is in effect?

I am unsure of the exact scoping rules of use strict; and would prefer to write a test encapsulating the exact rules.

Searching for this has proved difficult as there are 1.5 million articles extolling the virtues of use strict;

I understand I can turn it on and off with no strict;

  • Comment on Is there a way to determine if use strict; is in effect?

Replies are listed 'Best First'.
Re: Is there a way to determine if use strict; is in effect?
by tobyink (Canon) on Feb 07, 2014 at 22:04 UTC

    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

      Thank you for the complete answer, you nailed a lot of details and solved the overall question at the same time.

      This would mean that in my bad not-strict-using script I can start using classes I've defined with Moose without Moose's use strict; and use warnings; leaking out to the badly implemented script, which is awesome. Baby steps to sanity and all that :)

Re: Is there a way to determine if use strict; is in effect?
by Anonymous Monk on Feb 07, 2014 at 21:59 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1073924]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-16 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found