Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

The True but Zero value?

by Marshall (Canon)
on Mar 20, 2016 at 22:19 UTC ( #1158387=perlquestion: print w/replies, xml ) Need Help??

Marshall has asked for the wisdom of the Perl Monks concerning the following question:

I made a mistake in typing with a new text editor 0E0 got typed as 0EO, this still does work.

Anyway a useful thing to know, albeit embarrassing for me due to the typo. There is such a thing as "True" but numerically false!

The DBI returns "0E0" for the "true but zero" value. It does that when the query succeed but returned no rows. I haven't been writing Perl for awhile, so I seek guidance about how to handle this in recent Perl versions? There appears to be an extraneous warning when using arithmetic with a number represented as an exponential string.

#!/usr/bin/perl use warnings; use strict; $|=1; print "**TrueZeroValue = $TrueZeroValue\n"; if ($TrueZeroValue) { print "**This value is True\n"; } $TrueZeroValue += 0; #this used to have no warnings and #converted exponential string notation to #a numeric value if ($TrueZeroValue == 0) { print "**This value is zero numerically\n"; print "**but with current Perl, a warning happens\n"; } __END__ **TrueZeroValue = 0EO **This value is True Argument "0EO" isn't numeric in addition (+) at C:\Projects_Perl\TrueZ line 14. **This value is zero numerically **but with current Perl, a warning happens

Replies are listed 'Best First'.
Re: The True but Zero value?
by Anonymous Monk on Mar 20, 2016 at 22:34 UTC

    It happens, the important thing is that you updated your post :-)

    Interesting related factoid: Perl treats the string "0 but true" specially, it is exempt from the warnings about numeric conversion, so it is also true in boolean context but zero in numeric context. (see e.g. ioctl and fcntl)

      Yes, there is "0 but true" and that is very, very seldom known thing. You are correct about that extra detail that I'd forgotten, but I have seen it before.

      Thanks for adding that point!

      My main point got lost in that there is such a thing as "True but Zero value". I just blew it in my post. With my current small screen, number "0" and letter "O" look almost the same. I blew it. I admit it. Let's move on.

      Update: Here is some code that proves your comment:

      #!/usr/bin/perl use warnings; use strict; my $x = "0 but true"; my $y = "0 but whatever"; $x += 0; $y += 0; __END__ Prints: Argument "0 but whatever" isn't numeric in addition (+) at C:\Projects_Perl\ line 9. Process completed successfully
      You are correct, my $x = "0 but true"; is "special". 0E0 is more often seen, but this works the same way.

        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'); }


        13:25 >perl 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 li +ne 34. 0 BUT TRUE 0 true Argument "0 but true " isn't numeric in addition (+) at l +ine 34. 0 but true 0 true Argument "1 and true" isn't numeric in addition (+) at 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,

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1158387]
Approved by Athanasius
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2022-05-28 03:42 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (98 votes). Check out past polls.