This is an archived low-energy page for bots and other anonmyous visitors.
Please sign up if you are a human and want to interact.
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...
Re: How do I tell if strict, taint, etc are on?
by Fletch (Bishop) on Mar 16, 2006 at 14: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.
| [reply] [d/l] |
|
|
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.
| [reply] |
|
|
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.
| [reply] |
Re: How do I tell if strict, taint, etc are on?
by zer (Deacon) on Mar 16, 2006 at 15:17 UTC
|
the variables following state if they are on:
${^TAINT}
$^W # Warnings
for those who are searching and find this, take a look at Perl Special Variables Quick Reference | [reply] [d/l] |
|
|
Thanks; that helps. I feel foolish for missing that on the perlvar page.
| [reply] |
|
|
use warnings;
print($^W, "\n"); # 0 XXX
| [reply] [d/l] [select] |
Re: How do I tell if strict, taint, etc are on?
by DrWhy (Chaplain) on Mar 16, 2006 at 15: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.
--DrWhy
"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."
| [reply] |
Re: How do I tell if strict, taint, etc are on?
by ikegami (Patriarch) on Mar 16, 2006 at 17: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().
| [reply] [d/l] [select] |
Re: How do I tell if strict, taint, etc are on?
by linux454 (Pilgrim) on Mar 16, 2006 at 22:10 UTC
|
As for checking if use strict and use warnings has been used, try:
print "strict module loaded... ",
exists($INC{'strict.pm'}) ? "yes" : "no",
"\n";
print "warnings module loaded... ",
exists($INC{'warnings.pm'}) ? "yes" : "no",
"\n";
Others have addressed testing for Taint.
Update: fixed typo noticed by been42 | [reply] [d/l] |
|
|
print "strict module loaded... ",
exists($INC{'strict.pm'}) ? "yes" : "no",
"\n";
print "warnings module loaded... ",
exists($INC{'warnings.pm'}) ? "yes" : "no",
"\n";
| [reply] [d/l] |
|
|
Thanks for catching that.
| [reply] |
|
|