Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Supressing warnings

by Anonymous Monk
on Jul 22, 2010 at 13:50 UTC ( #850850=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I came across a few things with regard to the suppression of warnings that surprised me, so I thought I look for some enlightenment here...

I am using Perl 5.12 and Net::SNMP 6.0.0.

The following script produces 26 deprecation warnings:

#!/home/mh/perl512/bin/perl use Net::SNMP; # end of script1, warnings
Because the warnings come from another file you cannot simply disable them with "no warnings 'deprecated'", so if you want to get rid of them you can of course disable them globally with -X:
#!/home/mh/perl512/bin/perl -X use Net::SNMP; # end of script2, no warnings
Now I thought that you could achieve the same effect with $^W, so I had expected that this would also get rid of the warnings:
#!/home/mh/perl512/bin/perl BEGIN { $^W = 0 }; use Net::SNMP; # end of script3, warnings
but surprisingly it doesn't - this emits warnings. Why is that?

Also I had expected that if you would run "perl script2" (the script with the -X in the shebang-line) you would see the warnings again (as there is now no -X on the commandline) but surprisingly you don't. It seems the -X of the shebang-line takes effect even though you explicitly invoke perl on the command line. Is that so?

Finally I would be interested if there is a way to globally disable specifically the deprecation warnings only.

Many thanks!

Comment on Supressing warnings
Select or Download Code
Re: Supressing warnings
by morgon (Deacon) on Jul 22, 2010 at 13:53 UTC
    Sorry, posted my question anonymously by mistake.
    This comment is only for me to better find it later.
Re: Supressing warnings
by kennethk (Monsignor) on Jul 22, 2010 at 14:50 UTC
    but surprisingly it doesn't - this emits warnings. Why is that?

    My guess is that this has the same scope problems that no warnings 'deprecated' has.

    It seems the -X of the shebang-line takes effect even though you explicitly invoke perl on the command line. Is that so?

    From DESCRIPTION in perlrun:

    The #! line is always examined for switches as the line is being parsed.

    Finally I would be interested if there is a way to globally disable specifically the deprecation warnings only.

    I'm hoping someone can offer up a more elegant solution, as the following seems a little fragile/hackish. In order to filter out deprecation warnings, I've put in a BEGIN block with a second internal block with a localized WARN handler. Since perl compiles each module at most one time, this catches the emitted warnings on the first compile. The regular expression then filters out any newline-delimited portion of the emitted warnings that contain the word deprecated. Note that swapping from a require to a use will make the following code fail to catch the warnings, at least on my machine.

    BEGIN { my $initial_warnings = ""; { local $SIG{__WARN__} = sub{$initial_warnings .= $_[0]}; require Foo; } $initial_warnings =~ s/^.*deprecated.*$ \n?//xmg; warn $initial_warnings if $initial_warnings; } use Foo;
      The #! line is always examined for switches as the line is being parsed.

      Funny, I never knew that. It does not even need to contain a path to perl, all it needs is an occurance of the string "perl".

      If a.pl e.g. starts with "#!end lines properly! -d", then "perl a.pl" starts the debugger.

      I hope I find a way to turn this knowledge into a beer at the upcoming YAPC::Europe (even though I think this behavour does not really makes sense).

        I think this behavour does not really makes sense

        I'd say it makes a lot of sense :)

        (For example, at 5.8 times when there was no say, -l was one of my favorite options to put in the shebang line for short test scripts.)

Re: Supressing warnings
by toolic (Chancellor) on Jul 22, 2010 at 15:08 UTC
    I noticed that the deprecated warning is generated even when warnings are not in effect! This leads me to believe that this category of warning messages is not controlled by the usual means (-w, use warnings, etc.). Consider:
    $ perl -e 'sub pdu:locked:method { return 'pdu'} print pdu(), $/' Use of :locked is deprecated at -e line 1. 5 $ perl -e 'my @array; if (defined @array) { print 6 }' defined(@array) is deprecated at -e line 1. (Maybe you should just omit the defined()?)

    A little more digging leads me to perl5120delta, in its "Potentially Incompatible Changes" section: Deprecations warn by default

    Perl now defaults to issuing a warning if a deprecated language feature is used.

    The doc mentions a way to lexically disable the warning (as you have already tried), but there is no mention of a way to globally disable it.

    As you may be aware, there is already a bug report for this on Net::SNMP. The best solution, of course, is to fix the module. Perhaps you could be proactive and upload a patch to fix this. You would then have your own local copy without the warnings until the module author implements the fix.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://850850]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-11-28 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls