May use a "strict" warning instead of an error

When using use strict perl will generate a runtime error for unsafe constructs. Now I wonder if it is possible to print it only a warning, and not cause a runtime error? Or is there a use warnings (or -w) warning about the same issues?

+4
source share
5 answers

I'm going to take a hit to guess the real motivation here. Feel free to tell me if I am not mistaken.

I suspect that you are trying to tackle a large, old code base and would like to include restrictions, but you hoped to first understand where the errors will be (and how many there are), without breaking functionality. Unfortunately, since the use strict functions, changing the internal behavior of the Perl parser and interpreter, there is no "freely strict" or, similar to html, any type of "transition" mode.

However, you can split use strict functionality to start moving in the right direction. First, note that there are actually three separate parts:

 use strict 'refs'; # no symbolic references use strict 'vars'; # must declare variables use strict 'subs'; # no barewords 

and only those 'refs' generate runtime errors. Thus, you can easily add use strict qw(vars subs) to each of your files (scripts and modules) and test them with perl -c . If you encounter error messages, comment on use strict or at least depending on which of the two checks failed, and add a comment about the nature of the failure and proceed. Thus, you can quickly (depending on the number of files) determine which files have compile-time errors and return to 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 BEGIN blocks, this should be pretty safe.

The more complex part checks the runtime errors generated by use strict 'refs' and, unfortunately, there really is no easy way to do this, because errors are triggered using symbolic links that cannot be detected by any static analysis such as -c and / or Perl :: Critic are useless.

Hope this comes close to solving your real problem.

+4
source

No, use strict cannot be forced to issue warnings, not die. All he does is set a few bits in the magic variable $^H , which causes various things in the guts of the Perl interpreter.

No, use warnings does not warn you that use strict kills you. For example, use warnings warnings will warn you about variables used only once (which may be the result of typos).

+14
source

The warnings and strict warnings are complementary, not overlapping. The strict has both compile time and runtime. You cannot reduce stress from errors to warnings, but you can completely disable them. For example, if you are writing your own export procedure, you need to include symbolic links to manipulate the symbol table.

 { no strict 'refs'; # symrefs okay within this block } 

Warnings can also be disabled lexically (if you did use warnings instead of the very obsolete -w flag).

Lines and warnings provide a protective grid. Therefore, it is recommended that you use them by default. If you disable them, you should disable only what is needed and limit this change to the minimum possible amount.

+4
source

Preferred Method:

 use Carp; sub foo { croak "no args" unless @_; } eval foo(); if( $@ ){ print "caught die: $@ "; } 

If you cannot change die to croak :

 sub foo { die "no args" unless @_; } { my $prev_die = $SIG{__DIE__}; $SIG{__DIE__} = sub { print "caught die: $_[0]"; }; eval foo(); $SIG{__DIE__} = $prev_die; } 

The second method prints errors on STDERR.

Cm:

perldoc -f eval

perldoc perlvar and find /\$\@/ and /__DIE__/

perldoc Carp

+3
source

Warnings can be fatal; see perllexwarn - but severe errors cannot be made non-fatal.

Why would you want to do that? I suspect the problem is XY.

+1
source

Source: https://habr.com/ru/post/1345213/


All Articles