Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to determine if 'strict' is enabled

by kcott (Archbishop)
on Jun 07, 2023 at 01:59 UTC ( [id://11152671]=perlquestion: print w/replies, xml ) Need Help??

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

Background

I'm doing some testing of Perl::Dist::APPerl on various platforms. I have a test script which currently checks that 'use v5.36;' enables the warnings pragma and features are enabled or disabled as appropriate.

Question

I'm looking for a platform-independent way to determine if the strict pragma is in effect without forcing an abort.

If strict is specifically used, it can be determined from %INC:

ken@titan ~/tmp $ perl -e 'use strict; print "$_\n" for keys %INC;' strict.pm ken@titan ~/tmp $ perl -e 'use strict; my $x = xyz;' Bareword "xyz" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.

However, if strict is enabled automatically via use VERSION (where VERSION >= 5.12), this method doesn't work:

ken@titan ~/tmp $ perl -e 'use 5.012; print "$_\n" for keys %INC;' ken@titan ~/tmp $ perl -e 'use 5.012; my $x = xyz;' Bareword "xyz" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.

Any help with this would be appreciated. Thankyou.

— Ken

Replies are listed 'Best First'.
Re: How to determine if 'strict' is enabled
by ikegami (Patriarch) on Jun 07, 2023 at 03:58 UTC

    You can't use %INC.

    $ perl -le'no strict; print $INC{ "strict.pm" } ?1:0' 1 # False positive $ perl -le'{ use strict; } print $INC{ "strict.pm" } ?1:0' 1 # False positive $ perl -le'use JSON; print $INC{ "strict.pm" } ?1:0' 1 # False positive $ perl -le'use v5.20; print $INC{ "strict.pm" } ?1:0' 0 # False negative

    You can use ( caller(0) )[8] in a sub.

    $ perl -le' sub f { ( ( caller(0) )[8] & 0x602 ) == 0x602 } print f ?1:0; ' 0 $ perl -le' sub f { ( ( caller(0) )[8] & 0x602 ) == 0x602 } use strict; print f ?1:0; ' 1 $ perl -le' sub f { ( ( caller(0) )[8] & 0x602 ) == 0x602 } use v5.20; print f ?1:0; ' 1

    0x2 is strict refs, 0x200 is strict subs, and 0x400 is strict vars.

      This enlightened me.

      Is this documented anywhere ikegami? If so, it's something I've completely overlooked, glossed over or for some reason over the years completely ignored.

        G'day stevieb,

        "Is this documented anywhere ikegami?"

        I looked this up after ++ikegami's response. See caller for element-8, $hints, and subsequent discussion. See "strict's source code" for the numbers (0x2, 0x200, and 0x400).

        — Ken

      G'day ikegami,

      ++ Thanks. That's just the sort of thing I was looking for.

      — Ken

Re: How to determine if 'strict' is enabled
by tobyink (Canon) on Jun 12, 2023 at 09:34 UTC
    my $strict_enabled = do { my $code = sprintf( '$____DUMMY____%d = 1', 1_000_000 + int( rand 8_ +999_888 ) ); not eval( $code ); }; if ( $strict_enabled ) { print "strict vars\n"; } else { print "no strict vars\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-24 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found