http://qs1969.pair.com?node_id=1158394


in reply to Re^2: The True but Zero value?
in thread The True but Zero value?

Hello Marshall,

0E0 is more often seen, but this works the same way.

Yes, but only when it appears as a string; when it appears as a number, it is zero and false:

#! perl use strict; use warnings; for ( 0, 0E0, '0E0', 0.0, '0.0', 0.00, '0.00', 00, '00', '0', '0 but true', '0 BUT TRUE', '0 but true ', '1 and true', ) { printf "%-11s %d %s\n", $_, ($_ + 0), ($_ ? 'true' : 'false'); }

Output:

13:25 >perl 1575_SoPW.pl 0 0 false 0 0 false 0E0 0 true 0 0 false 0.0 0 true 0 0 false 0.00 0 true 0 0 false 00 0 true 0 0 false 0 but true 0 true Argument "0 BUT TRUE" isn't numeric in addition (+) at 1575_SoPW.pl li +ne 34. 0 BUT TRUE 0 true Argument "0 but true " isn't numeric in addition (+) at 1575_SoPW.pl l +ine 34. 0 but true 0 true Argument "1 and true" isn't numeric in addition (+) at 1575_SoPW.pl li +ne 34. 1 and true 1 true 13:25 >

The string '0' is documented as special: it is zero and false. Other strings are true1 regardless of their numerical values. It appears that the isn't numeric warning applies only when the numerical conversion is not “pure” (because the string contains non-numeric characters).

It seems strange that this behaviour is so poorly documented. The '0 but true' special case is not mentioned in perlsyn#Truth-and-Falsehood. My experiments suggest that this special string must be character-for-character exact for the special behaviour to apply. For example, '0 but true ' (note the extra space at the end) still generates an isn't numeric warning.

Update: 1Except the empty string, '', which is also documented to be false.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,