# SPUG: 0 == undef

Peter Darley pdarley at kinesis-cem.com
Wed Apr 7 14:00:51 CDT 2004

```Tim,
Further examination shows that any non-numeric value==0, so I guess I'll
cook up an IsNumber() function and do if (IsNumber(\$Test1) &&
IsNumber(\$Test2) && \$Test1==\$Test2).  Or perhaps I'll make a function like
Equal(\$Test1, \$Test2).  None of this will help with readability tho. :)
Any idea why this is set up like this?
Thanks,
Peter Darley

-----Original Message-----
From: Tim Maher [mailto:tim at consultix-inc.com]
Sent: Wednesday, April 07, 2004 11:44 AM
To: Peter Darley
Cc: SPUG
Subject: Re: SPUG: 0 == undef

On Wed, Apr 07, 2004 at 11:26:43AM -0700, Peter Darley wrote:
> Folks,
> 	I'm wondering if there's any way to get 0 to not equal undef.  When I do:
>
> my (\$Test1, \$Test2) = 0, undef;
> if (\$Test1 == \$Test2) {print "Crap!\n"}
> else {print "OK\n"}
>
> 	I find out that 0==undef.
>
> 	I'm also wondering what the thinking here is?
> Thanks,
> Peter Darley

When you use an undefined value in a numeric comparison,
Perl fudges in a 0 value for you (and gives you a warning,
if -w is enabled).

If you want to differentiate between undef values and
those with zero values, you should use the "defined" function.

my (\$Test1, \$Test2) = 0, undef;
if ( ! defined \$Test1 or ! defined \$Test2 ) {
print "Sorry, can't compare numbers, at least one is
undefined\n"; )
}
elsif (\$Test1 == \$Test2) {print "Equal!\n"}

==============================================================
| Tim Maher, Ph.D.                    tim(AT)teachmeperl.com |
| SPUG Leader Emeritus               spug(AT)teachmeperl.com |
| Seattle Perl Users Group        http://www.seattleperl.com |
| SPUG Wiki Site               http://spugwiki.perlocity.org |
| Perl Certification Site      http://perlcert.perlocity.org |
==============================================================

```