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


In reply to Re: no module warnings in client code by kcott
in thread no module warnings in client code by citromatik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":