有什么用!!(双感叹号)是什么意思? [英] What does !! (double exclamation point) mean?

查看:81
本文介绍了有什么用!!(双感叹号)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,来自 Alias 的博客文章,我注意到使用了双感叹号!!.我想知道它意味着什么,以及我将来可以去哪里找到像这样的 Perl 语法的解释.(是的,我已经在 perlsyn 上搜索了 !!).

package Foo;使用 vars qw{$DEBUG};开始 {$DEBUG = 0 除非定义了 $DEBUG;}使用常量 DEBUG =>!!$调试;子 foo {调试('在子 foo')如果调试;...}

更新
感谢您的所有回答.

这是我刚刚发现的与 The List Squash Operator x!!

解决方案

这只是两个 ! 布尔运算符,而不是彼此相邻的运算符.

使用这个习惯用法是为了确保您收到一个 1 或一个 0.实际上它返回一个空字符串,该字符串数字化为 0.不过它通常只用于数字或布尔上下文.

您经常会在 Code Golf 比赛,因为它比使用三元 更短?: 运算符与 10 ($test ? 1 : 0).

<预><代码>!!未定义 == 0!!0 == 0!!1 == 1!!$obj == 1!!100 == 1未定义?1 : 0 == 00 ?1 : 0 == 01 ?1 : 0 == 1$obj ?1 : 0 == 1100 ?1 : 0 == 1

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

这篇关于有什么用!!(双感叹号)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆