How about this:
( defined($p) ? '1' . $p : 0 ) eq ( defined($q) ? '1' . $q : 0 )
For example:
use strict; use warnings; my %strings = ( 'undef' => undef, 'empty' => '', 'string' => '0' ); my @pairs = ( [ 'undef' , 'undef' ], [ 'undef' , 'empty' ], [ 'undef' , 'string' ], [ 'empty' , 'empty' ], [ 'empty' , 'string' ], [ 'string', 'string' ], ); for ( @pairs ) { my ($p,$q) = @strings{@$_}; my $cmp = ( (defined($p) ? '1' . $p : 0) eq (defined($q) ? '1' . $q : 0) ) ? 'true' : 'false'; print join(" eq ", @$_), " = $cmp\n"; }
Result:
undef eq undef = true undef eq empty = false undef eq string = false empty eq empty = true empty eq string = false string eq string = true
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
In reply to Re: Comparing undefined values
by xdg
in thread Comparing undefined values
by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |