我注意到looks_like_number 并不像我假设的那样简单地返回真/假,而是实际上返回一个字节,指示perl 内部说存储在标量中的数字类型。例如:
perl -e'use Scalar::Util qw/looks_like_number/; for (qw/ 1 3 10 34.23 545435.234 2343.0 234 -1423 1sddf -865178652134876152348761253487613254 sdf 24363456345636534563567253765734655 8764325hjkh435 iuh340874 &*^*& 786521948761324876132497821347816.23452345 -8762135487126387432.12435154243 0 nan inf/) { print $_, ": ", looks_like_number($_), "\n" } '
1: 1
3: 1
10: 1
34.23: 5
545435.234: 5
2343.0: 5
234: 1
-1423: 9
1sddf: 0
-865178652134876152348761253487613254: 10
sdf: 0
24363456345636534563567253765734655: 2
8764325hjkh435: 0
iuh340874: 0
&*^*&: 0
786521948761324876132497821347816.23452345: 6
-8762135487126387432.12435154243: 14
0: 1
nan: 36
inf: 20
它实际上并没有记录在我可以找到的 Scalar::Util 中,只是提到它返回 perlapi 的looks_like_number 值,这也没有在文档中。乍一看,它似乎是:
- & 1 = 数字
- & 2 = 64 位
- & 4 = 浮点数
- & 8 = 负数
- & 16 = 无穷大
- & 32 = 不是数字
这些掩码在代码中是否可移植且安全使用?