baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi

problem is how to achieve selective warnings in the script.

example:

use warnings; use strict; if (!$r){ do something }
the problem is that $r is dynamically initialized, sometimes it is there and sometimes it isn't. so during the times it isn't i get the warnings that there is nothing there (as it should be). so i wan't to turn warnings off for this special case.

thanx

Replies are listed 'Best First'.
Re: selective warnings
by bellaire (Hermit) on Mar 02, 2009 at 13:06 UTC
    Why not just check if it's defined before testing its boolean value?
    if (defined $r && !$r){ do something }
Re: selective warnings
by moritz (Cardinal) on Mar 02, 2009 at 13:19 UTC
    What warnings do you get exactly? Surely not from the if (!$r):
    $ perl -le 'use warnings; if (!undef) { print "foo" }' foo

    No warning from !undef.

    So first you should find out what's the cause for the warning.

    Then you can read perllexwarn on how to disable warnings selectively.

Re: selective warnings
by Bloodnok (Vicar) on Mar 02, 2009 at 13:08 UTC
    If it's dynamically initialised, then why not...
    use warnings; use strict; # Give the compiler an indication the variable may be used use vars qw/$r/; if (!$r){ do something }

    A user level that continues to overstate my experience :-))
Re: selective warnings
by davido (Cardinal) on Mar 02, 2009 at 17:55 UTC

    Warnings are generally there to reduce the risk of you getting yourself into coding trouble, and to let you know that you might be doing something that has unintended consequences. A best practice would be to write clean enough code that you don't trigger warnings. If you are getting a warning, you might want to look at your approach and see if there's something that can be done to clean it up.

    Very occasionally you might be doing something intentionally that triggers a warning. In such cases there is the "no warnings qw/warnings list/;" pragma, which can be used to lexically shut off a particular warning. When doing so it is advisable to disable the specific warning in the smallest possible lexical scope so that you still gain the benefit of that warning being active everywhere else in your code.

    But for the most part disabling warnings should not be used as a crutch enabling you to go on coding without bothering to learn "a better way." It's a little like the advice many give regarding symbolic references; just because they exist and you can use them doesn't mean that your situation is the special case that tips the "pros/cons" scale in favor of your using them. Nevertheless, Perl gives us symbolic refs so that those who have a true need can use them. Turning off a specific warning in a narrow scope is perfectly fine, provided it's not being done so that you can write buggy and unmaintainable code.

    With that introduction, the required reading for selective warnings is perllexwarn -- a document that discusses lexical scoping for warnings and provides a tree of possible warnings.


    Dave