Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Turn off warning for undefined value in comparison?

by traveler (Parson)
on Mar 03, 2004 at 23:10 UTC ( #333725=perlquestion: print w/ replies, xml ) Need Help??
traveler has asked for the wisdom of the Perl Monks concerning the following question:

I looked at perllexwarn, but I cannot figure out what to tell warnings. I really don't like to write
if ( $foo and $foo ne "bar")
when really I want just
if ( $foo ne "bar" )
I mean, if it is undefined, it sure is not equal to "bar". So I'd like to turn off just that warning. I cannot figure out (other then by trial and error, I guess) what category to turn off. So, what do I tell "warnings" to turn off just the "undefined in comparison" check? Thanks.

Comment on Turn off warning for undefined value in comparison?
Select or Download Code
Re: Turn off warning for undefined value in comparison?
by Limbic~Region (Chancellor) on Mar 03, 2004 at 23:16 UTC
    traveler,
    Check out perldoc perldiag. Remember that by using the warnings pragma instead of the -w command line option that you can toggle settings on a per scope basis.
    #!/usr/bin/perl use strict; use warnings; no warnings 'uninitialized'; my $foo; print "cool daddy-o\n" if $foo eq 'bar';
    Cheers - L~R
Re: Turn off warning for undefined value in comparison?
by waswas-fng (Curate) on Mar 03, 2004 at 23:17 UTC
    use strict; use warnings; my $foo = ""; if ( $foo ne "bar" ) { print "lala\n"; }
    Why is it undefined in the first place?


    -Waswas
Re: Turn off warning for undefined value in comparison?
by traveler (Parson) on Mar 03, 2004 at 23:45 UTC
    Thanks Limbic. I did not know that there was a list there, too. perllexwarn was the only list for 'use warnings' that I could find.

    waswas-fng in my case I have a hash of values that comes from XML::Simple. If a given tag does not have a parameter e.g.<foo>blah</foo> vs. <foo bar="x">blah</foo> the value for $xml->{bar} is undefined.

    --traveler

Re: Turn off warning for undefined value in comparison?
by cLive ;-) (Parson) on Mar 04, 2004 at 00:41 UTC

    I really don't like to write...

    Why not? It can help you remember context of a var and whether or not it may be undefined at that point.

    I would use:

    if ( defined $foo and $foo ne "bar")

    to remind myself that $foo could be undefined, or, when assigning $foo:

    # depending on context my $foo = whatever() || ''; my $foo = whatever() || 0;

    provided $foo as an empty string/zero is not going to cause you problems. If $foo could be valid as either in a comparison, you need to go the define route (or turn off warnings - yuck!).

    .02

    cLive ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-11-01 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    If a safe, affordable anti-ageing treatment that extended life indefinitely were to become available, would you take it?



    Results (4 votes), past polls