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

查看:46
本文介绍了“0 但为真"是什么意思?在 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 (), 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天全站免登陆