有人能解释一下 Perl 中字符串“0 but true”的确切含义吗?据我了解,它在整数比较中等于零,但在用作 boolean 值时计算结果为 true。它是否正确?这是语言的正常行为还是解释器中将其视为特殊情况的特殊字符串?
最佳答案
这是语言的正常行为。引用 perlsyn
联机帮助页:
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”
就是为了这个目的。
关于perl - "0 but true"在 Perl 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5318011/