0, 0e0, 0.0, -0, +0, 000 对 Perl 来说都意味着同样的事情,为什么?
只是让我感到困惑。
相关但不同的问题: Perl 中的“0 but true”是什么意思?
Just puzzling to me.
Related, but different question:
What does “0 but true” mean in Perl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Perl 不区分数字的种类。从非 CS/程序员的角度来看,它们对我来说也意味着同样的事情:零。 (这是 Perl 的基础之一:它试图像人一样工作,而不是像计算机一样工作。“如果它看起来像一只鸭子......”)
所以,如果你将它们用作数字,它们都是相同的东西。如果将它们用作字符串,它们会有所不同。这确实会导致您可能需要强制采用一种解释的情况(“0 但正确”;另请参阅"nancy键入”)。但总的来说,它会自动“做正确的事”。
Perl doesn't distinguish kinds of numbers. Looking at all of those with a non-CS/programmer eye, they all mean the same thing to me as well: zero. (This is one of the foundations of Perl: it tries to work like people, not like computers. "If it looks like a duck....")
So, if you use them as numbers, they're all the same thing. If you use them as strings, they differ. This does lead to situations where you may need to force one interpretation ("0 but true"; see also "nancy typing"). but by and large it "does the right thing" automatically.
我不明白,他们还应该是什么意思?
您可以给出整数、科学数、浮点数、有符号整数和零的八进制表示法。为什么它们应该不同?
I don't understand, what else should they mean?
You give integer, scientific, floating point, signed integers and octal notations of zero. Why should they differ?
0==0
众所周知,包括拉里·沃尔在内。0==0
as everyone, including Larry Wall, knows.Perl 将每个标量值解释为字符串和(可能)数字。根据 perl 的转换规则,所有这些零的字符串表示形式都可以转换为整数值 0 :
"0", "0.0", "-0", "+0", "000" =>直接字符串到数字转换的最简单情况。
“0e0”=>在数字上下文中,仅转换前导有效数字字符,因此仅使用前导“0”。例如,“1984abcdef2112”在数字上将被解释为 1984。perl
中的“0 but true”意味着像“0e0”这样的字符串将在数字上计算为 0,但在布尔上下文中将为“true”,因为转换为布尔值如下与严格的数字转换不同的规则。
Perl interprets every scalar value as both a string and (potentially) a number. All of those string representations of zero can convert to the integer value 0 , according to perl's conversion rules:
"0", "0.0", "-0", "+0", "000" => Simplest case of straight string to numeric conversion.
"0e0" => In a numeric context, only the leading valid numeric characters are converted, so only the leading "0" is used. For example, "1984abcdef2112" would be interpreted numerically as 1984.
"0 but true" in perl means that a string like "0e0" will evalutate numerically to 0, but in a boolean context will be "true" because the conversion to boolean follows different rules than the strict numeric conversion.
Perl 在上下文中工作。在字符串上下文中,它们都是不同的。在数字上下文中,它们都为零。
布尔上下文中的 '0 but true' 为 true:
Perl works in contexts. In string context, they are all different. In numeric context, they are all zero.
'0 but true' in boolean context is true: