Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: no module warnings in client code

by kcott (Archbishop)
on Aug 27, 2013 at 14:55 UTC ( [id://1051148]=note: print w/replies, xml ) Need Help??


in reply to no module warnings in client code

G'day citromatik,

In the first instance, you'd probably want to consider why you don't want to see the warnings. Can you rewrite your code so that you don't get warnings in the first place? I'd also question why you want to prevent all warnings (i.e. with no warnings;) rather than just a specific warning (e.g. no warnings 'once';). If you think that the warnings that are emitted are in some way wrong, can you raise a bug report? Can you tell us what actual module you're dealing with? — perhaps we can suggest a fix for your code or some other workaround.

Given this module:

package Bad; use warnings; sub gen_warning { print "Undeclared: >>>$x<<<"; } 1;

There's clearly a bug (albeit highly contrived) which should be fixed.

Using it normally:

$ perl -le ' use Bad; Bad::gen_warning(); ' Use of uninitialized value $Bad::x in concatenation (.) or string at B +ad.pm line 6. Undeclared: >>><<<

Looking at the documentation for the warnings pragma, you'll see:

"The warnings pragma is a replacement for the command line flag -w, but the pragma is limited to the enclosing block, ..." [my strong emphasis]

This is why your posted "something like this" suggestion doesn't work:

$ perl -le ' use Bad; { no warnings; Bad::gen_warning(); } ' Use of uninitialized value $Bad::x in concatenation (.) or string at B +ad.pm line 6. Undeclared: >>><<<

However, as an interim measure, you could do something like this:

$ perl -le ' use Bad; { no warnings "redefine"; sub Bad::gen_warning { use warnings "redefine"; no warnings "uninitialized"; print "Undeclared: >>>$x<<<"; } } Bad::gen_warning(); ' Undeclared: >>><<<

Notice how I turned off one specific warning to redefine the subroutine; that particular warning is switched back on inside the subroutine (obviously unnecessary for this simple example: I'm making a point about affecting the smallest possible scope when doing this). Also, inside the subroutine, only one specific type of warning is turned off.

Probably the biggest problem with that is you'll always be stuck with your version! When the offending subroutine is fixed, enhanced, extended or otherwise modified, your code will continue to use your redefinition.

If you provide more information about your specific issue, perhaps we can suggest a better way to deal with it.

-- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found