Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

can i disable certain warnings?

by jrsmith (Pilgrim)
on Jun 20, 2000 at 22:38 UTC ( #19062=perlquestion: print w/ replies, xml ) Need Help??
jrsmith has asked for the wisdom of the Perl Monks concerning the following question:

the title says it all.. specifically i am trying to get rid of these "Use of uninitalized value at blah blah". i know i can get around it by checking if the value is defined first but hey i'm lazy, so if such a thing exists i'd appreciate it if you could enlighten me. thanx :)

Comment on can i disable certain warnings?
Re: can i disable certain warnings?
by btrott (Parson) on Jun 20, 2000 at 22:45 UTC
    Perl 5.6 contains the ability to only turn on certain warnings, I believe, using some configuration of the
    use warnings;
    pragma. I've not used 5.6, but you can take a look at perllexwarn.

    Looking at it, you should be able to use

    no warnings qw/uninitialized/;
    In general, though, you may want to check out *why* you're getting the warnings. I know that sounds obvious, but oftentimes it suggests that you've got an problem, or that you should do more error checking.
RE: can i disable certain warnings?
by Shendal (Hermit) on Jun 20, 2000 at 22:49 UTC
    Although I'd recommend fixing the problem, not the symptom, I think you want something like this:
    use warnings; no warnings qw(uninitialized);
    See the warnings pragma documentation for more information. This is described in the standard perl FAQ, under the heading of perllexwarn.
Re: can i disable certain warnings?
by jrsmith (Pilgrim) on Jun 20, 2000 at 23:02 UTC
    well it's just from blank spaces being read from my input sources, and i can't seem to get rid of all of them becuz the format the sources are in seem to change each time we get a new one it's really frustrating... i'll try your suggestions, thanx a lot
      What I do to make sure data is going to be in the vars that come in I usually use:
      if (! $myvalue ){ $myvalue = $defaultValue; }
      If this is what you are asking.

      --BigJoe
        Another way to write this is:
        $myvalue ||= $defaultValue;
        ...of course, if $myvalue had a "0" as a legitgimate value, you would want something like:
        defined $myvalue or $myvalue=$defaultValue;
Re: can i disable certain warnings?
by Adam (Vicar) on Jun 21, 2000 at 00:25 UTC
    As several of the posts have said, it is better to just fix the source of the warning. If you mask out that warning, you won't get it later when you edit your file later and make a mistake. Its well worth spending the time now to find a way to make it work. Maybe check to see if vars are defined first, like:<code> if( defined ($var ) ) { # use $var }
Re: can i disable certain warnings?
by Adam (Vicar) on Jun 21, 2000 at 00:25 UTC
    As several of the posts have said, it is better to just fix the source of the warning. If you mask out that warning, you won't get it later when you edit your file later and make a mistake. Its well worth spending the time now to find a way to make it work. Maybe check to see if vars are defined first, like:
    if( defined ($var ) ) { # use $var }
RE: can i disable certain warnings?
by Russ (Deacon) on Jun 21, 2000 at 06:50 UTC
    I have found myself using this trick (I resolved, for one client, to give them code which caused no warnings whatsoever under normal usage):
    # When retrieving data from DBI my $Ref = {map(defined $_ ? $_ : '', %{$sth->fetchrow_hashref})};
    This feels a bit odd, but is easier than only checking the values of the hash. Of course, you can do the same with arrays...
    my $Ref = [map(defined $_ ? $_ : '', @{$sth->fetchrow_arrayref)];

    Since I am also lazy, I found this much better than always writing:

    print "<td> @{[ defined $Var ? $Var : '' ]} </td>";
    Oh, well. Just sharing some more of my strange code... :-)

    Russ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2014-09-19 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls