Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^7: The True but Zero value?

by demerphq (Chancellor)
on Apr 11, 2016 at 16:53 UTC ( #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.

---
$world=~s/war/peace/g

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1160137]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2020-09-23 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (130 votes). Check out past polls.

    Notices?