如何区分Perl中的数字标量和字符串标量?
问题描述:
Perl通常将数字转换为字符串值,反之亦然。然而,必须有一些允许例如 Data :: Dumper
以区分两者,如下例所示:
Perl usually converts numeric to string values and vice versa transparently. Yet there must be something which allows e.g. Data::Dumper
to discriminate between both, as in this example:
use Data::Dumper;
print Dumper('1', 1);
# output:
$VAR1 = '1';
$VAR2 = 1;
是否有一个Perl函数允许我以类似的方式区分标量的值是否存储为数字或字符串?
Is there a Perl function which allows me to discriminate in a similar way whether a scalar's value is stored as number or as string?
答
使用纯perl无法找到它。 Data :: Dumper使用C库来实现它。如果强制使用Perl,如果字符串看起来像十进制数字,它就不会区别于数字。
There's no way to find this out using pure perl. Data::Dumper uses a C library to achieve it. If forced to use Perl it doesn't discriminate strings from numbers if they look like decimal numbers.
use Data::Dumper;
$Data::Dumper::Useperl = 1;
print Dumper(['1',1])."\n";
#output
$VAR1 = [
1,
1
];