Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

How do I tell if strict, taint, etc are on?

by barrachois (Pilgrim)
on Mar 16, 2006 at 19:43 UTC ( #537259=perlquestion: print w/replies, xml ) Need Help??
barrachois has asked for the wisdom of the Perl Monks concerning the following question:

The title pretty much says it all.

While I can see how to turn strict-ness on or off, and how to invoke perl with taint mode enabled, I don't see how to tell from within running code what the status of these various pragmas and options.

There's a Test::Taint CPAN module that includes some tricks for telling if taint mode is on or not - so I guess I see one way that can be done. But it sure isn't pretty.

I feel like I'm missing something simple...

  • Comment on How do I tell if strict, taint, etc are on?

Replies are listed 'Best First'.
Re: How do I tell if strict, taint, etc are on?
by zer (Deacon) on Mar 16, 2006 at 20:17 UTC
      Not quite:
      use warnings; print($^W, "\n"); # 0 XXX
      Thanks; that helps. I feel foolish for missing that on the perlvar page.
Re: How do I tell if strict, taint, etc are on?
by Fletch (Chancellor) on Mar 16, 2006 at 19:47 UTC

    You might could weasel it out of one of the special variables ($^H looks remotely promising; see perlvar), but then the question that springs to mind is: Why? Tainting I could see possibly (you want to not run unless it's enabled), but a use for runtime checking of strict-ness eludes me.

      Yeah, I see $^H, thanks, but since the docs say explicitly "WARNING: This variable is strictly for internal use only", it doesn't sound exactly like a supported API. :)

      As to why, well, I'm working on a diagnostic/debugging page for some mod_perl web stuff, that shows me the environment, database handles, process status, and so on. As part of that, I wanted it to report as much as it could about the state of the running perl environment - including the various pragmas and what-not.

        Ahh. OK I can see that. :)

        A problem that comes to mind right off is that strict is lexically scoped so you're probably not going to be able to tell from outside a given scope if it's on or off inside, at least as far as I can think of. You might be able to do some deep magic with one of the B modules, but I wouldn't count on it. If no one else here comes up with anything you might run this by p5p and see if anyone there can offer pointers.

Re: How do I tell if strict, taint, etc are on?
by ikegami (Pope) on Mar 16, 2006 at 22:18 UTC

    You can check for taint checking using $^T (documented in perlvar).

    There's no such thing as strict being on or off. There are three kinds of strict checks which are independant. You can check their status by causing a violation in an eval:

    my $strict_refs = not eval { no warnings; ${'var'}; 1 }; my $strict_subs = not eval 'no warnings; ThisDoesNotExist; 1'; my $strict_vars = not eval 'no warnings; $ThisDoesNotExist; 1';

    Similar to strict checks, there's no such thing as warnings being on or off. There are many kinds of warning checks which are independant. $^W will tell you if -w on the command line (or equivalent), but each and all individual warnings can be enabled and disabled using warnings without affecting $^W. You can check if a particular warning is enabled using warnings::enabled().

Re: How do I tell if strict, taint, etc are on?
by linux454 (Pilgrim) on Mar 17, 2006 at 03:10 UTC
    As for checking if use strict and use warnings has been used, try:
    print "strict module loaded... ", exists($INC{''}) ? "yes" : "no", "\n"; print "warnings module loaded... ", exists($INC{''}) ? "yes" : "no", "\n";
    Others have addressed testing for Taint.

    Update: fixed typo noticed by been42

      A slight correction:

      print "strict module loaded... ", exists($INC{''}) ? "yes" : "no", "\n"; print "warnings module loaded... ", exists($INC{''}) ? "yes" : "no", "\n";
        Thanks for catching that.
Re: How do I tell if strict, taint, etc are on?
by DrWhy (Chaplain) on Mar 16, 2006 at 20:39 UTC
    For strictness, you could look to see if $strict::VERSION is defined. This will only tell you whether strict has been used at some point in the code. It won't tell you whether any strictness is actually in effect at the point in the code you do the checking. For more specifics, I don't see anything better than interrogating $^H.


    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://537259]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-06-24 21:02 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.