It seems comparison undef ne undef returns undef. Does Perl have idiomatic way to convert it to perls "boolean"?
Also I find following to be strange:
undef ne undef || 0; # 0
(undef ne undef) || 0; # undef
Could you explain, please, what is happening?
It doesn't.
nereturns either the special scalartrue(known as&PL_yesinternally) or the special scalarfalse(&PL_no), neverundef. In this particular case, it returnsfalse.(The results are the same in earlier versions of Perl, but
is_boolwas only introduced in Perl v5.36.)falseis a scalar that contains the integer zero, the float zero and the empty string.That's because it's not true. The two snippets are 100% equivalent, and they both return
0.(The results are the same in earlier versions of Perl, but
is_boolwas only introduced in Perl v5.36.)neis a string comparison operator. So it starts by casting its operands into strings. Convertingundefto a string produces the empty string, and emits a warnings. Since the empty string is equal to the empty string,nereturns a false value, and specifically the specialfalsescalar.Since
falseis a false value,||evalutes and returns its right-hand side value (0).The simplest way to get
trueorfalsefrom a value is to negate it twice.The simplest way to get
1if true or0if false is to use the conditional operator.