问题描述
这是奇怪的。
以下内容:
This is strange.The following:
$sum = !0;
print $sum;
打印出1如你所愿。但这种
prints out 1 as you would expect. But this
$sum = !1;
print $sum;
打印出什么。为什么呢?
prints out nothing. Why?
推荐答案
注意:你写的不是做你觉得它在做什么。请记住,Perl有没有真正的布尔数据类型。它有标量,散列,列表和引用。它处理真/假值的方式,那么,是上下文。一切都计算为true在Perl除了不确定变量,则空单,空字符串,而数字0。
Be careful: what you've written isn't doing what you think it's doing. Remember, perl has no real boolean datatype. It's got scalars, hashes, lists, and references. The way it handles true/false values, then, is contextual. Everything evaluates to "true" in perl except for undefined variables, the empty list, the empty string, and the number 0.
你的code是干什么的,然后,走的是一条价值的计算结果为假的倒数,它可以是任何东西这是不是在上面的列表中。按照惯例,并为简单起见,PERL返回1(虽然你不应该依赖于;它很可能返回一个包含一系列的随机号码清单,因为这将评估为真为好)
What your code is doing, then, is taking the inverse of a value that evaluates to "false", which can be anything which is not in the list above. By convention and for simplicity's sake, perl returns 1 (though you should not rely on that; it could very well return a list containing a series of random numbers, because that will evaluate to "true" as well.)
当你问一个值的计算结果为逆类似的事情发生真。什么是真正被打印出来,不是什么,它是空字符串(''),其中,正如我所说,在评估前布尔pressions为假。您可以检查这一点:
A similar thing happens when you ask for the inverse of a value that evaluates to "true." What's actually being printed out is not "nothing," it's the empty string (''), which, as I mentioned, evaluates to "false" in boolean expressions. You can check this:
print "This evaluates to false\n" if( (!1) eq '');
如果你问为什么perl的吐出来的是空字符串,而不是其他的假值中的一个,那么,这可能是因为是用perl作出处理字符串,这是一个完全合理的字符串交还。
If you're asking for why perl spits out the empty string instead of one of the other "false" values, well, it's probably because perl is made to handle strings and that's a perfectly reasonable string to hand back.
这篇关于为什么!1给我什么在Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!