Perl 如何决定将标量视为字符串或数字?
考虑以下代码及其输出:
代码
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $HOURS_PER_DAY = 24.0 * 1.0;
my $BSA = 1.7 * 1.0;
my $MCG_PER_MG = 1000.0 * 1.0;
my $HOURS_DURATION = 20.0 * $HOURS_PER_DAY;
my $dummy = $HOURS_PER_DAY * $BSA * $MCG_PER_MG * $HOURS_DURATION;
print Dumper($HOURS_PER_DAY);
print Dumper( $BSA);
print Dumper( $MCG_PER_MG);
print Dumper( $HOURS_DURATION );
输出
$VAR1 = 24;
$VAR1 = '1.7';
$VAR1 = 1000;
$VAR1 = 480;
正如您所看到的,第二个变量被视为字符串,而第一个和第四个变量被视为数字。 有人知道底层逻辑是什么吗?
添加的编辑算术计算并不能完全解决问题(请参阅 $BSA 变量)。
$ perl -v
This is perl, v5.10.0 built for cygwin-thread-multi-64int
(with 6 registered patches, see perl -V for more detail)
Copyright 1987-2007, Larry Wall
Consider the following code and its output:
Code
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $HOURS_PER_DAY = 24.0 * 1.0;
my $BSA = 1.7 * 1.0;
my $MCG_PER_MG = 1000.0 * 1.0;
my $HOURS_DURATION = 20.0 * $HOURS_PER_DAY;
my $dummy = $HOURS_PER_DAY * $BSA * $MCG_PER_MG * $HOURS_DURATION;
print Dumper($HOURS_PER_DAY);
print Dumper( $BSA);
print Dumper( $MCG_PER_MG);
print Dumper( $HOURS_DURATION );
Output
$VAR1 = 24;
$VAR1 = '1.7';
$VAR1 = 1000;
$VAR1 = 480;
As you can see, the second variable is treated as strings, while the first and the forth ones are treated as numbers. Anybody has any idea what is the underlying logic?
Edit arithmetic calculations that were added do not completely solve the problem (see the $BSA variable).
$ perl -v
This is perl, v5.10.0 built for cygwin-thread-multi-64int
(with 6 registered patches, see perl -V for more detail)
Copyright 1987-2007, Larry Wall
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Data::Dumper 的工作是序列化数据,并且您无法根据其输出了解 perl 在内部对数据执行的操作。 Devel::Peek 模块可以转储存储在变量中的底层标志和值。 Devel::Peek POD 解释了标志的重要性。
Data::Dumper's job is to serialize data and you can't tell much about what perl is doing internally with the data based on its output. The Devel::Peek module can dump the underlying flags and values stored in the variables. The Devel::Peek POD explains the significance of the flags.
Perl 将变量视为字符串或数字的整个概念是有缺陷的。 当您需要时,Perl 会以正确的方式处理您的变量,例如,算术运算符总是将其参数视为数字(假设您没有滥用运算符重载或类似操作)。
您不应该担心这一点:Perl 知道它在做什么。
Your whole concept of Perl treating variables as strings or numbers is flawed. Perl will treat your variables the right way when you need it to, for example, arithmetic operators always take treat their argument as numbers (assuming you aren't abusing operator overloading or some such).
You shouldn't worry about this: Perl knows what it's doing.
Data::Dumper 在变量的字符串表示形式上使用简单的模式来确定它是否是数字。 从源代码来看:
这与带有小数点的数字不匹配,这解释了您观察到的行为。
Data::Dumper is using a simple pattern on the string representation of the variable to determine if it is a number. From the source code:
This doesn't match numbers that have a decimal point which explains the behavior you observed.
强制数字上下文的快速肮脏方法:
如果您关心的是数据将如何显示,那么干净的方法是:-
Quick dirty way to force a numeric context:
If your concern is how the data will be displayed then the clean way is:-