Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

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.

like image 352
jm666 Avatar asked Jun 17 '12 20:06

jm666


People also ask

How do I turn on warnings in Perl?

Warnings in Perl can be created by using a predefined function in Perl known as 'warn'. A warn function in Perl generates a warning message for the error but will not exit the script. The script will keep on executing the rest of the code.

What is use strict and use warnings in Perl?

Strict and Warning are probably the two most commonly used Perl pragmas, and are frequently used to catch “unsafe code.” When Perl is set up to use these pragmas, the Perl compiler will check for, issue warnings against, and disallow certain programming constructs and techniques.


1 Answers

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();
like image 125
choroba Avatar answered Oct 26 '22 16:10

choroba