SPUG: 0 == undef
Tim Maher
tim at consultix-inc.com
Wed Apr 7 13:43:42 CDT 2004
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 |
==============================================================
More information about the spug-list
mailing list