Search code examples
perl

How to get the set of warning checks currently enabled in the perl module?


In the perllexwarn are defined all warnings what is possible to set.

But here is nothing about, how to print out what warnings i have currently enabled.

E.g.:

use strict;
use warnings;

print warnings::enabled->pretty_print(); #fictional...

How is it possible?

example:

use strict;
use 5.012;
use warnings;

my $aaa;
say "$aaa";

say warnings::enabled("uninitialized") ? "yes" : "no";

The above will output:

Use of uninitialized value $aaa in string at y line 6.

no

so, the "uninitialized" warning category is "set", because its prints a warning, but the warnings::enabled("uninitialized") not returns true.


Solution

  • Reading perllexwarn

    ... functions that are useful for module authors. These are used when you want to report a module-specific warning to a calling module has enabled warnings via the "warnings" pragma.

    If I understand it correctly, it means the functions (enabled, warnif) only work for module-specific warnings, not for the standard categories. (There is probably a missing "that" before "has" in the documentation.)

    Update: It seems standard categories work as well, but only in a module:

    package MY;
    use warnings::register;
    sub S {
        my $x;
        print $x, "\t";
        print warnings::enabled("uninitialized"),"\n";
    }
    
    package main;
    use warnings;
    MY::S();
    no warnings;
    MY::S();