develooper Front page | perl.perl5.porters | Postings from June 2003

q/0/ is false but q/0.0/ is true

Thread Next
From:
Dan Kogai
Date:
June 28, 2003 11:04
Subject:
q/0/ is false but q/0.0/ is true
Message ID:
EBCA0D1D-A992-11D7-8FA5-000393AE4244@dan.co.jp
Porters,

I came up with this thought when I was filing a complaint to a web site 
which states perl's notion of (true|false) wrong.

I don't think this is a bug but I found it rather inconsistent q/0.0/ 
is true while q/0/ is false.

% perl -le '$a=0 ; print $a ? "t" : "f"'
f
% perl -le '$a=0.0 ; print $a ? "t" : "f"'
f
% perl -le '$a="0" ; print $a ? "t" : "f"'
f
% perl -le '$a="0.0" ; print $a ? "t" : "f"'
t

Another oddity similar to this is that numification does not grok 
nondecimals while it is smart enough to grok even floating point 
decimals.

% perl -le '$a = "1"; print $a+0'
1
% perl -le '$a = "1e1"; print $a+0'
10
% perl -le '$a = ".1"; print $a+0'
0.1
% perl -le '$a = ".1e2"; print $a+0'
10
% perl -le '$a = "012"; print $a+0' # parsed as decimal, not octal
12
% perl  -le '$a = "0x12"; print $a+0'
0
% perl -le '$a = "0b1"; print $a+0'
0

Of course you can  eval qq// and/or 'use override' but I just wonder 
how much performance penalty we have to pay if we make perl's string 
(bool|num)ification as smart as perl parser itself.

"Dan the Perl5 Porter"+0; # Dan the Nonnumeral Man


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About