Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^7: The True but Zero value?

by demerphq (Chancellor)
on Apr 11, 2016 at 16:53 UTC ( [id://1160137] : note . print w/replies, xml ) Need Help??

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

I should have mentioned in my original post that there is another set of special cases for this purpose. Perl silently ignores leading and trailing whitespace when numifying a string (which AFAIK is not widely known nor documented, although a pointer to the relevant docs are welcome). So there are an infinite set of strings which are false numerically, but true in boolean context. (And an infinite set of strings which are numerically equivalent to any integer you choose.)

perl -wle'my $n= (" " x 1_000_000) . "0" . (" " x 1_000_000); print $n + ? "true" : "false", " as a bool, prenumification"; print 0+$n ? "tru +e" : "false", " as a bool, numified"; print $n ? "true" : "false", " +as a bool, postnumification";' true as a bool, prenumification false as a bool, numified true as a bool, postnumification

This is not generally a problem except for people who write serializers and need to ensure that " 0 " round trips correctly.


Replies are listed 'Best First'.
Re^8: The True but Zero value?
by Marshall (Canon) on Apr 11, 2016 at 18:04 UTC
    You are correct about "numifying" a string and ignoring white space. I made a recent post that demo's this at: Re: confusion with Chomp. I don't know where this is documented either, but I know that it is the way that it works.