问题描述
在下面的代码中,来自 Alias 的 博文,我注意到使用了双感叹号 !!
.我想知道这意味着什么,以及将来我可以去哪里寻找这样的 Perl 语法的解释.(是的,我已经在 perlsyn 搜索了 !!
.
In the code below, from a blog post by Alias, I noticed the use of the double exclamation point !!
. I was wondering what it meant and where I could go in the future to find explanations for Perl syntax like this. (Yes, I already searched for !!
at perlsyn).
package Foo;
use vars qw{$DEBUG};
BEGIN {
$DEBUG = 0 unless defined $DEBUG;
}
use constant DEBUG => !! $DEBUG;
sub foo {
debug('In sub foo') if DEBUG;
...
}
更新
感谢您的所有回答.
UPDATE
Thanks for all of your answers.
这是我刚刚发现的其他相关列表壁球运算符x!!代码>
Here is something else I just found that is related The List Squash Operator x!!
推荐答案
这只是两个 !
布尔而不是相邻的运算符.
It is just two !
boolean not operators sitting next to each other.
使用这个成语的原因是确保你收到一个1
或一个0
.实际上它返回一个空字符串,它 numifys 为 0.不过它通常只用于数字或布尔上下文.
The reason to use this idiom is to make sure that you receive a 1
or a 0
. Actually it returns an empty string which numifys to 0. It's usually only used in numeric, or boolean context though.
您会经常在 Code Golf 比赛,因为它比使用三元 更短?:
运算符与 1
和 0
.
You will often see this in Code Golf competitions, because it is shorter than using the ternary ? :
operator with 1
and 0
.
!! undef == 0
!! 0 == 0
!! 1 == 1
!! $obj == 1
!! 100 == 1
undef ? 1 : 0 == 0
0 ? 1 : 0 == 0
1 ? 1 : 0 == 1
$obj ? 1 : 0 == 1
100 ? 1 : 0 == 1
这篇关于什么!!(双感叹号)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!