为什么Perl认为-1是真的?

时间:2021-07-30 16:48:59

This is a piece of common example code:

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

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

which prints 'foo' forever.

永远打印'foo'。

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:

有人可以解释为什么这是一个有用的实现吗?这至少适用于5.10,Unix和MacOS X:

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

which gives

这使

foo
foo
foo
...

7 个解决方案

#1


18  

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.

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

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.

无论如何,Perl认为那些意味着“空”或“无”的价值是错误的。大多数语言采用类似的观点。具体来说,整数零,浮点零,字符串零,空字符串和undef都是false。

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

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

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)

#2


19  

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

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

#3


16  

From perldoc perlsyn (Truth and Falsehood):

来自perldoc perlsyn(真理和虚假):

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

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

-1 is considered true.

-1被认为是真的。

#4


4  

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

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

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

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

  • 0
  • 0
  • undef
  • *基金
  • '' (empty string)
  • ''(空字符串)

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

这就是为什么我能想到一个合适的答案。它就是这样设计的。

#5


4  

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

只有0整数被认为是假的。任何其他非零整数都被认为是真的。

#6


2  

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

任何整数<> 0都是真的。 0总是假的。

#7


1  

Perl took this behavior from awk and C.

Perl从awk和C中采取了这种行为。

Why C does it is explained here.

为什么C会这样解释。

#1


18  

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.

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

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.

无论如何,Perl认为那些意味着“空”或“无”的价值是错误的。大多数语言采用类似的观点。具体来说,整数零,浮点零,字符串零,空字符串和undef都是false。

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

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

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)

#2


19  

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

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

#3


16  

From perldoc perlsyn (Truth and Falsehood):

来自perldoc perlsyn(真理和虚假):

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

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

-1 is considered true.

-1被认为是真的。

#4


4  

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

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

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

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

  • 0
  • 0
  • undef
  • *基金
  • '' (empty string)
  • ''(空字符串)

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

这就是为什么我能想到一个合适的答案。它就是这样设计的。

#5


4  

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

只有0整数被认为是假的。任何其他非零整数都被认为是真的。

#6


2  

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

任何整数<> 0都是真的。 0总是假的。

#7


1  

Perl took this behavior from awk and C.

Perl从awk和C中采取了这种行为。

Why C does it is explained here.

为什么C会这样解释。