“0 但真实"是什么意思?在 Perl 中是什么意思? [英] What does "0 but true" mean in Perl?
问题描述
谁能解释一下字符串0 but true"在 Perl 中的确切含义?据我了解,它在整数比较中等于零,但在用作布尔值时评估为真.这个对吗?这是语言的正常行为,还是在解释器中被视为特殊情况的特殊字符串?
Can someone explain what exactly the string "0 but true" means in Perl? As far as I understand, it equals zero in an integer comparison, but evaluates to true when used as a boolean. Is this correct? Is this a normal behavior of the language or is this a special string treated as a special case in the interpreter?
推荐答案
这是语言的正常行为.引用 perlsyn
手册页:
It's normal behaviour of the language. Quoting the perlsyn
manpage:
数字0
,字符串'0'
和""
,空列表()
,和 undef
在布尔上下文中都是错误的.所有其他值都为真.否定!
或 not
的真值返回一个特殊的假值.当评估为字符串时,它被视为 ""
,但作为数字,它是视为0
.
The number
0
, the strings'0'
and""
, the empty list()
, andundef
are all false in a boolean context. All other values are true. Negation of a true value by!
ornot
returns a special false value. When evaluated as a string it is treated as""
, but as a number, it is treated as0
.
因此,需要有一种方法从系统调用中返回 0
,该系统调用期望将 0
作为(成功的)返回值返回,并留下一个通过实际返回错误值来指示失败案例的方法.0 but true"
就是为了这个目的.
Because of this, there needs to be a way to return 0
from a system call that expects to return 0
as a (successful) return value, and leave a way to signal a failure case by actually returning a false value. "0 but true"
serves that purpose.
这篇关于“0 但真实"是什么意思?在 Perl 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!