“0 但真实"是什么意思?在 Perl 中是什么意思? [英] What does "0 but true" mean in Perl?

查看:41
本文介绍了“0 但真实"是什么意思?在 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 (), and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value. When evaluated as a string it is treated as "", but as a number, it is treated as 0.

因此,需要有一种方法从系统调用中返回 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆