When using use strict
perl will generate a runtime error on unsafe constructs. Now I am wondering if it is possible to have it only print a warning instead of causing a runtime error ? Or is use warnings
(or -w) warning about the same issues ?
相关问题
- $ENV{$variable} in perl
- How do I identify what code is generating “ '&
- Is it possible to pass command-line arguments to @
- Redirecting STDOUT and STDERR to a file, except fo
- Change first key of multi-dimensional Hash in perl
相关文章
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- <link> onerror do not work in IE
- Can NOT List directory including space using Perl
- Extracting columns from text file using Perl one-l
- Could not find default endpoint element that refer
- Lazy (ungreedy) matching multiple groups using reg
- How do I tell DBD::mysql where mysql.sock is?
Warnings can be made fatal — see perllexwarn — but strict errors can't be made non-fatal.
Why do you want to do that? I suspect an XY problem.
The preferred method:
If you can't change your
die
's tocroak
's:The second method will print out the errors on STDERR.
See:
perldoc -f eval
perldoc perlvar
and search for/\$\@/
and/__DIE__/
perldoc Carp
I'm gonna take a stab at guessing the real motivation here. Feel free to tell me if I guessed wrong.
I suspect your trying to tackle a large, older code base and would like to enable strictures but you were hoping first to get a sense of where the errors will be (and how many there are) without breaking functionality. Unfortunately, since
use strict
functions by modifying the internal behavior of the perl parser and interpreter, there isn't a 'loose strict' or, by analogy to html, any kind of 'transitional' mode.However, you can tease apart the functionality of
use strict
to start moving in the right direction. First, note that there are actually three separate parts:and of those only 'refs' generates runtime errors. So you could easily add
use strict qw(vars subs)
to each of your files (scripts and modules) and test them withperl -c
. If you encounter any error messages, then comment out theuse strict
, or at least whichever of the two checks failed, and add a comment as to the nature of the failure and move on. This way you can quickly (depending on the number of files) determine which files have compile-time errors and come back to address them later. (If you were more motivated than me at the moment, you could even automate this process). Unless you have code that does scary things inside ofBEGIN
blocks, this should be pretty safe to do.The trickier part is checking for the runtime errors generated by
use strict 'refs'
and unfortunately, there really isn't an easy way to do this because the errors are triggered by symbolic references which can't be determined by any kind of static analysis so -c and/or Perl::Critic are both useless.Hopefully that gets closer to addressing your real problem.
The
warnings
andstrict
pragmas are complementary, not overlapping. Thestrict
pragma has both compile-time and run-time effects. You can't reduce the severity of strictures from errors to warnings, but you can disable them entirely. For example, if you're writing your own export routine you'll need to enable symbolic references in order to manipulate the symbol table.Warnings can also be disabled lexically (assuming you did
use warnings
instead of the largely obsolete-w
flag).Strictures and warnings provide a safety net. That's why they're recommended to be used by default. If you disable them you should disable only what's necessary and limit the change to the smallest possible scope.
No,
use strict
can't be made to issue warnings rather than die. All it does is set a few bits in the magic$^H
variable, which triggers various things in the guts of the Perl interpreter.No,
use warnings
isn't warning about the same things asuse strict
kills you for. For instance,use warnings
will warn you about variables used only once (which might be the result of typos).