go ahead... be a heretic PerlMonks

### Re^2: True or False? A Quick Reference Guide

by demerphq (Chancellor)
 on Sep 29, 2005 at 11:21 UTC ( #496040=note: print w/replies, xml ) Need Help??

in reply to Re: True or False? A Quick Reference Guide
in thread True or False? A Quick Reference Guide

Interesting. I wasn't aware of "0 but true" being special cased. But you did realize that strings matching /^0E\d+\$/ have this property too right?

```D:\>perl -lwe "my @b=(0,1); for (qw(0E0 1E0)) { print; print qq(Bool:)
+,\$b[!!\$_]; print qq(0+:),0+\$_; print \$/; }"
0E0
Bool:1
0+:0

1E0
Bool:1
0+:1
---
\$world=~s/war/peace/g

Replies are listed 'Best First'.
Re^3: True or False? A Quick Reference Guide
by xdg (Monsignor) on Sep 29, 2005 at 19:16 UTC

Yes, though that's a little more logical, since perldata specifies that numbers can be written in scientific notation in a form like 12.34E-10. So 0E0 is just a number, and "0E0" is a string that has length (is true) but evaluates as 0E0, which is zero (false). If you want to think about it the other way, '0' is a special case of a string that has length but is treated as false.

What I find interesting about "0 but true" is that it more clearly highlights to a reader that something unusual is going on than something like "0.0" or "0E0" -- and to such an extent that it was excepted from the usual warnings so as to make it easier to use that way. Anyone know the history of this? Or what version of Perl it first appeared in? (timtoady, are you out there?)

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Create A New User
Node Status?
node history
Node Type: note [id://496040]
help
Chatterbox?
 [Tanktalus]: clobbering common::sense altogether looks smarter than what I did :) [Tanktalus]: also, would it not have been a simpler patch to try to get the XS code compiled in C99+ mode for all the comments? :) [Tux]: C99 is not supported on HP-UX before 11.31 [Tux]: at least not reliable. And perl itself requires C89 (but probes if it can safely use some C99 features) Eily takes a peek in the last hour of CB to discover that LanX woke up is boobs with some Queen :P

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-09-20 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
During the recent solar eclipse, I:

Results (237 votes). Check out past polls.

Notices?