check if two possibly undef values differ
am 02.02.2005 09:50:32 von Nils Boysen
hi,
what is the simplest way to compare two values that might be undef
preserving undef?
I want to preserve undef so I can't use ($x || '') && ($y || '')
the simplest way I could come up with is this:
#!/usr/bin/perl -w
use strict;
my $x = undef;
my $y = undef;
if((defined $x && !defined $y) || (!defined $x && defined $y)
|| (defined $x && defined $y && $x ne $y)) {
print "differ\n";
}
there has to be a way to write this simpler - is there?
thanx.
Re: check if two possibly undef values differ
am 03.02.2005 19:10:57 von Roar Sunde
On 2005-02-02, Nils Boysen wrote:
> hi,
>
> what is the simplest way to compare two values that might be undef
> preserving undef?
>
> I want to preserve undef so I can't use ($x || '') && ($y || '')
>
> the simplest way I could come up with is this:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> my $x = undef;
> my $y = undef;
>
> if((defined $x && !defined $y) || (!defined $x && defined $y)
> || (defined $x && defined $y && $x ne $y)) {
> print "differ\n";
> }
>
> there has to be a way to write this simpler - is there?
>
> thanx.
This should do it.
{
local $SIG{'__WARN__'} = sub {};
print "differ\n" unless $x eq $y;
}