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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.