“0 但为真"是什么意思?在 Perl 中是什么意思? [英] What does "0 but true" mean in Perl?
问题描述
有人能解释一下 Perl 中字符串0 but true"的确切含义吗?据我了解,它在整数比较中等于零,但在用作布尔值时计算为真.这个对吗?这是语言的正常行为还是解释器中将其视为特殊情况的特殊字符串?
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屋!