On Fri, 19 Oct 2001, Matt Bernstein wrote:
> how about ($x+0 eq $x)
>
> (rather untested)
We can have a look with a test set taken from Amelia (page 59-60) :
perl -ne 'chomp;print+($_+0eq$_)?"":"NOT","a number\n"'
12345
a number
12345.67
a number
6.02e23
NOT a number
4_294_967_296
NOT a number
0377
NOT a number
0xffff
NOT a number
0b1100_0000
NOT a number
Naturally, the last four should not really work anyway, since they are
understood by the parser/compiler, and have string representations that
look like numbers (print $a=0377 actually prints 255).
By the way, the negative interger work (almost) :
0
a number
-12
a number
-0.4
a number
-.5
NOT a number
.05
NOT a number
In fact, if you use the same string representation that Perl uses (that is
to say, do not use strings given by a user, but variables Perl has already
"touched"), it works rather well :
1.05e+23
a number
-1.05e-24
a number
--
Philippe "BooK" Bruhat
There is no greater magic than knowing exactly who and what you are.
(Moral from Groo #2 (Image))