Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to determine if 'strict' is enabled

by ikegami (Patriarch)
on Jun 07, 2023 at 03:58 UTC ( #11152672=note: print w/replies, xml ) Need Help??


in reply to How to determine if 'strict' is enabled

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.

Replies are listed 'Best First'.
Re^2: How to determine if 'strict' is enabled
by stevieb (Canon) on Jun 07, 2023 at 05:16 UTC

    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

        And especially $^H is enlightening, too.

          Each time a statement completes being compiled, the current value of $^H is stored with that statement, and can later be retrieved via (caller($level))[8]

        so it also depends at what time you need to check the strictness.

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

Re^2: How to determine if 'strict' is enabled
by kcott (Archbishop) on Jun 07, 2023 at 04:31 UTC

    G'day ikegami,

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

    — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2023-11-30 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?