Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How can I suppress 'uninitialized' warnings in a CPAN module?

by zork42 (Monk)
on Nov 17, 2013 at 17:25 UTC ( #1063005=note: print w/ replies, xml ) Need Help??


in reply to How can I suppress 'uninitialized' warnings in a CPAN module?

Thank you everyone for your comments!

I suppose the easiest solution would be to edit Tokenizer.pm, but part of me thinks that is a Bad Thing! ;)

I tried LanX's generic solution and it seems to work very well:

use warnings; use strict; p(); print "Test 1\n\n"; print undef; # "Use of uninitialized value in print at . +.." print 'a'+1; # "Argument "a" isn't numeric in addition ( ++) at ..." foo(1); p(); { local $SIG{__WARN__} = \&suppress_UV_warning; # <-------- Is th +is correct ? p(); print "Test 2\n\n"; print undef; # SUPPRESSED! "Use of uninitialized value i +n print at ..." print 'a'+1; # "Argument "a" isn't numeric in addition ( ++) at ..." foo(2); p(); } p(); print "Test 3\n\n"; print undef; # "Use of uninitialized value in print at . +.." print 'a'+1; # "Argument "a" isn't numeric in addition ( ++) at ..." foo(3); p(); # generate some more warnings to check they are suppressed here too sub foo { my ($x) = @_; print "\n\n"; print undef; # SUPPRESSED IN TEST 2! "Use of uninitializ +ed value in print at ..." print "foo $x" + 1; # "Argument "foo n" isn't numeric in additi +on (+) at ..." } # suppress "Use of uninitialized value" warnings only sub suppress_UV_warning { my ($message) = @_; if ($message =~ /^Use of uninitialized value/) { return; } warn $message; # <-------- Is this correct ? } # print separating line sub p { print "\n", '-'x100, "\n"; }
Produces this output:
---------------------------------------------------------------------- +------------------------------ Test 1 Use of uninitialized value in print at D:\test\test.pl line 7. Argument "a" isn't numeric in addition (+) at D:\test\test.pl line 8. 1 Use of uninitialized value in print at D:\test\test.pl line 38. Argument "foo 1" isn't numeric in addition (+) at D:\test\test.pl line + 39. 1 ---------------------------------------------------------------------- +------------------------------ ---------------------------------------------------------------------- +------------------------------ Test 2 Argument "a" isn't numeric in addition (+) at D:\test\test.pl line 19. 1 Argument "foo 2" isn't numeric in addition (+) at D:\test\test.pl line + 39. 1 ---------------------------------------------------------------------- +------------------------------ ---------------------------------------------------------------------- +------------------------------ Test 3 Use of uninitialized value in print at D:\test\test.pl line 27. Argument "a" isn't numeric in addition (+) at D:\test\test.pl line 28. 1 Use of uninitialized value in print at D:\test\test.pl line 38. Argument "foo 3" isn't numeric in addition (+) at D:\test\test.pl line + 39. 1 ---------------------------------------------------------------------- +------------------------------
I have 2 questions, are these 2 lines correct?
line 14 : local $SIG{__WARN__} = \&suppress_UV_warning;
line 50 : warn $message;


Comment on Re: How can I suppress 'uninitialized' warnings in a CPAN module?
Select or Download Code
Replies are listed 'Best First'.
Re^2: How can I suppress 'uninitialized' warnings in a CPAN module?
by LanX (Canon) on Nov 17, 2013 at 23:04 UTC
    > I have 2 questions, are these 2 lines correct?

    Correct yes but...

    > line 14 : local $SIG{__WARN__} = \&suppress_UV_warning;

    Personally I prefer anonymous subs if I never need to call them by name.

    > line 50 : warn $message;

    You should check the Perl-version, IIRC this causes an endless loop in Perl older < 5.8 (?) (Too lazy to look up when it was exactly fixed)

    Newer versions are fool proof. ;-)

    update

    Talking about "generic solutions":

    Please be aware that this approach doesn't help if the module in question is also recklessly overwriting $SIG{__WARN__}

    Therefore a safe way of overriding is to create a wrapper, which calls the overridden handler from within (if present in $SIG{__WARN__}).

    But you can only be sure after checking the source code of the module in question.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063005]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2015-07-28 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls