K
kj
I've read the PODs (warnings, perllexwarn) but I still don't know
how to solve this problem. Here's a toy version of it.
First some random module:
# Foo.pm
package Foo;
use strict;
use warnings;
# calling foo() will result in an 'uninitialized' warning
sub foo { my %h; exists $h{ $_[ 0 ] } }
1;
__END__
The function Foo::foo represents one of those situtations in which
the author of the module, for whatever reason, has not taken steps
to avoid a default warning.
Next, my script
use strict;
use Foo;
{ no warnings; Foo::foo(); }
exit;
Now, despite the "no warnings" in the script, when I run it I still
get the "Use of initialized value in exists" warning.
I know that I can silence the warnings by setting $SIG{__WARN__}
in the script to a suitable handler, but this leads to parsing the
text of warning messages, which is an invitation for bugs, and
basically throws the nice warnings pragma warnings hierarchy out
the window. (If there's a way to define $SIG{__WARN__} to mimic
the effects of "no warnings 'foobar'" without having to parse
warning messages in order to *guess* whether a particular warning
belongs to the "foobar" category, please let me know.)
Of course, I could remove "use warnings" from Foo.pm (or pester
its author to do so), then the warnings would *never* show up even
if the script were changed to
use strict;
use Foo;
{ use warnings; Foo::foo(); }
exit;
This is not good. Why? Because module authors are fallible like
everyone else, and despite their best efforts, a situation warranting
a warning will slip through, so it is nice to make those warnings
visible. I.e. putting "use warnings" at the top of each module
file seems to me like a fine idea. Except that then the script
*can't* turn off warnings!
I am at a loss. It seems that either alternative (including "use
warnings" in Foo.pm or leaving it out) leads to an undesirable
behavior.
IMO, the calling executable should have the final say on which
warnings get emitted. Is there a way to do this without having to
resort to defining $SIG{__WARN__} and parsing warning messages?
Thanks!
kj
how to solve this problem. Here's a toy version of it.
First some random module:
# Foo.pm
package Foo;
use strict;
use warnings;
# calling foo() will result in an 'uninitialized' warning
sub foo { my %h; exists $h{ $_[ 0 ] } }
1;
__END__
The function Foo::foo represents one of those situtations in which
the author of the module, for whatever reason, has not taken steps
to avoid a default warning.
Next, my script
use strict;
use Foo;
{ no warnings; Foo::foo(); }
exit;
Now, despite the "no warnings" in the script, when I run it I still
get the "Use of initialized value in exists" warning.
I know that I can silence the warnings by setting $SIG{__WARN__}
in the script to a suitable handler, but this leads to parsing the
text of warning messages, which is an invitation for bugs, and
basically throws the nice warnings pragma warnings hierarchy out
the window. (If there's a way to define $SIG{__WARN__} to mimic
the effects of "no warnings 'foobar'" without having to parse
warning messages in order to *guess* whether a particular warning
belongs to the "foobar" category, please let me know.)
Of course, I could remove "use warnings" from Foo.pm (or pester
its author to do so), then the warnings would *never* show up even
if the script were changed to
use strict;
use Foo;
{ use warnings; Foo::foo(); }
exit;
This is not good. Why? Because module authors are fallible like
everyone else, and despite their best efforts, a situation warranting
a warning will slip through, so it is nice to make those warnings
visible. I.e. putting "use warnings" at the top of each module
file seems to me like a fine idea. Except that then the script
*can't* turn off warnings!
I am at a loss. It seems that either alternative (including "use
warnings" in Foo.pm or leaving it out) leads to an undesirable
behavior.
IMO, the calling executable should have the final say on which
warnings get emitted. Is there a way to do this without having to
resort to defining $SIG{__WARN__} and parsing warning messages?
Thanks!
kj