为什么 Perl 认为 -1 为真?

发布于 2024-12-07 13:22:42 字数 449 浏览 0 评论 0原文

这是一段常见的示例代码:

while (1) { 
    print "foo\n"; 
}

它永远打印“foo”。

perl foo.pl
foo
foo
foo
...

while (0) { print "foo\n"; } 

如您所愿静静地死去:

perl test.pl

有人能解释为什么这是 while 的一个有用的实现吗?这至少适用于 5.10、Unix 和 MacOS X

while (-1) { print "foo\n"; }

foo
foo
foo
...

This is a piece of common example code:

while (1) { 
    print "foo\n"; 
}

which prints 'foo' forever.

perl foo.pl
foo
foo
foo
...

and

while (0) { print "foo\n"; } 

dies quietly as you expect:

perl test.pl

Can someone explain why this is a useful implementation of while? This works on 5.10 at least, Unix and MacOS X:

while (-1) { print "foo\n"; }

which gives

foo
foo
foo
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尤怨 2024-12-14 13:22:42

每个非零整数的计算结果都是true。 0 总是

Every non-zero integer evaluates to true. And 0 is always false

朕就是辣么酷 2024-12-14 13:22:42

如果有的话,可以说 -11 更有可能为真,因为 -1 (111..111b code>) 是零的按位求反 (000..000b)。 BASIC 和 GW-BASIC 在需要返回真值时使用 -1。

无论如何,Perl 认为表示“空”或“无”的值是错误的。大多数语言都持有类似的观点。具体来说,整数零、浮点零、字符串零、空字符串和 undef 为 false。

这是记录,尽管文档措辞不佳。 (它将 () 列为 false 值,但没有这样的值。)

除了一致性之外,采用这种方法非常有用。例如,它允许人们使用

if (@x)

而不是

if (@x != 0)

If anything, one could say -1 is more likely to be true than 1 since -1 (111..111b) is the bitwise negation of zero (000..000b). BASIC and GW-BASIC used -1 when they needed to return a true value.

Regardless, Perl decided that values that mean "empty" or "nothing" are false. Most languages take a similar view. Specifically, integer zero, floating point zero, the string zero, the empty string and undef are false.

This is documented, although the documentation is poorly worded. (It lists () as a value that's false, but there is no such value.)

Aside from consistency, it's very useful to take this approach. For example, it allows one to use

if (@x)

instead of

if (@x != 0)
假装不在乎 2024-12-14 13:22:42

来自perldoc perlsyn(真相与谎言)

数字 0、字符串 '0' 和 '' 、空列表 () 和 undef
在布尔上下文中都是 false。所有其他值均为 true。

-1 被认为是 true。

From perldoc perlsyn (Truth and Falsehood):

The number 0, the strings '0' and '' , the empty list () , and undef
are all false in a boolean context. All other values are true.

-1 is considered true.

葮薆情 2024-12-14 13:22:42

问题是“为什么 perl 认为 -1 是真的?”。。

答案是当 Perl 开发时,决定某些值将评估为 false。这些是:

  • 0
  • undef
  • '' (空字符串)

这就是我能想到的关于原因的合适答案。它就是这样设计的。

The question is 'why does perl think -1 is true?'.

The answer is when perl was developed it was decided that certain values would evaluate to false. These are:

  • 0
  • undef
  • '' (empty string)

That is all I can think of a a suitable answer as to why. It was just designed that way.

白况 2024-12-14 13:22:42

只有 0 整数才被视为 false。任何其他非零整数都被视为 true。

Only a 0 integer is considered false. Any other non-zero integer is considered true.

蓝戈者 2024-12-14 13:22:42

任意整数<> 0 为真。
0 始终为假。

any integer <> 0 is true.
0 is always false.

世界等同你 2024-12-14 13:22:42

Perl 从 awkC 继承了这种行为。

这里解释了为什么C这样做

Perl took this behavior from awk and C.

Why C does it is explained here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文