解决方法
使用Scalar :: Util :: looks_like_number(),它使用内部Perl C API的looks_like_number()函数,这可能是最有效的方法。
注意,字符串“inf”和“无穷大”被视为数字。
注意,字符串“inf”和“无穷大”被视为数字。
例:
#!/usr/bin/perl use warnings; use strict; use Scalar::Util qw(looks_like_number); my @exprs = qw(1 5.25 0.001 1.3e8 foo bar 1dd inf infinity); foreach my $expr (@exprs) { print "$expr is",looks_like_number($expr) ? '' : ' not'," a number\n"; }
给出此输出:
1 is a number 5.25 is a number 0.001 is a number 1.3e8 is a number foo is not a number bar is not a number 1dd is not a number inf is a number infinity is a number
也可以看看:
[perldoc Scalar::Util][1] [perldoc perlapi][2]