What does !! (double exclamation point) mean?

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.

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.

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.

You will often see this in Code Golf competitions, because it is shorter than using the ternary ? : ? : operator with 1 and 0 ( $test ? 1 : 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

not-not.

It converts the value to a boolean (or as close as Perl gets to such a thing).


因为其他三个答案都声称范围是“0”或“1”,所以我想我会提到Perl中的布尔值(由像==这样的运算符返回, not等等)是undef1 ,而不是01

链接地址: http://www.djcxy.com/p/72894.html

上一篇: 现有的标准样式和编码标准文件

下一篇: 什么! (双重感叹号)是什么意思?