Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you figure out how to turn off a specific warning

Tags:

perl

I've got some legacy code that I'm dealing with. It's too much to clean up in one shot. It's using \1 inside of the s/// operator. I looked into perllexwarn and found I can shut it off with no warnings qw(syntax), but I did this with trial and error. Is there an easier way to get right from the warning to the way to shut it off?

It's doing stuff like this:

use strict;
$_ = "abc";
s/abc/\1/;
no warnings qw(syntax);
s/abc/\1/;

the message it generates is :

\1 better written as $1
like image 367
kdubs Avatar asked Sep 01 '25 01:09

kdubs


2 Answers

Execute your script as

perl -Mdiagnostics ./a.pl

or temporarily add use diagnostics; to your script. This will produce something like

\1 better written as $1 at ./a.pl line 4 (#1)
    (W syntax) Outside of patterns, backreferences live on as variables.
    The use of backslashes is grandfathered on the right-hand side of a
    substitution, but stylistically it's better to use the variable form
    because other Perl programmers will expect it, and it works better if
    there are more than 9 backreferences.

Notice the (W syntax)? The word is the warning class for which you are looking.

diagnostics gets its information from perldiag, which you could search manually instead of using use diagnostics;.


Other examples:

$ perl -Mdiagnostics -we'print undef'
Use of uninitialized value in print at -e line 1 (#1)
    (W uninitialized) An undefined value was used as if it were already
    [...]

$ perl -Mdiagnostics -we'no warnings qw( uninitialized ); print undef'

$ perl -Mdiagnostics -we'sub foo { } sub foo { }'
Subroutine foo redefined at -e line 1 (#1)
    (W redefine) You redefined a subroutine.  To suppress this warning, say
    [...]

$ perl -Mdiagnostics -we'no warnings qw( redefine ); sub foo { } sub foo { }'

$

In case you're curious, the letter provided by diagnostics before the warning class is one of the following:

  • (W) A warning (optional).
  • (D) A deprecation (enabled by default).
  • (S) A severe warning (enabled by default).
  • (F) A fatal error (trappable).
  • (P) An internal error you should never see (trappable).
  • (X) A very fatal error (nontrappable).
  • (A) An alien error message (not generated by Perl).
like image 63
ikegami Avatar answered Sep 03 '25 21:09

ikegami


I'd make a global signal handler, set it in a BEGIN block so it's compiled in early, and skip only the warning you don't want, so you still get any potential unexpected and unrelated warnings (even within the same category, due to not having to disable the whole thing):

use warnings;
use strict;

BEGIN {
    $SIG{__WARN__} = sub {
        my $warn = shift;
        return if $warn =~ /\\\d better written as/;
        warn $warn;
    };
}

my $x = 'abc';
$x =~ s/(abc)/\1/;

warn "a different warning\n";

Output:

a different warning
like image 40
stevieb Avatar answered Sep 03 '25 22:09

stevieb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!