The following code may do what you want:
use strict;
use warnings;
my @pairs =
(
[undef, undef], [0, undef], [0, 0], ['', undef], ['', ''], [1, und
+ef], [1, 1],
[0, '0'], [0, 0], ['0', '0'], ['0e0', '0'], ['x', '0'], ['x', 0],
);
for (@pairs)
{
my @pair = (@$_);
print defined $pair[0] ? ">$pair[0]<" : 'undef';
print ', ' . (defined $pair[1] ? ">$pair[1]<" : 'undef');
print ': ' . (equals (@$_) ? "match\n" : "different\n");
}
sub equals {
my ($lhs, $rhs) = @_;
#False if mix of defined and undef
return 0 if defined $lhs != defined $rhs;
# True if both undef
return 1 if ! defined $lhs && ! defined $rhs;
# False if different as numbers
no warnings "numeric";
return 0 if (0 + $lhs) != (0 + $rhs);
# False if different as strings
return 0 if ('' . $lhs) ne ('' . $rhs);
return 1;
}
Prints:
undef, undef: match
>0<, undef: different
>0<, >0<: match
><, undef: different
><, ><: match
>1<, undef: different
>1<, >1<: match
>0<, >0<: match
>0<, >0<: match
>0<, >0<: match
>0e0<, >0<: different
>x<, >0<: different
>x<, >0<: different
DWIM is Perl's answer to Gödel