Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!

like image 234
Christopher Bottoms Avatar asked Jan 30 '10 15:01

Christopher Bottoms


People also ask

Is double exclamation rude?

No, I do not consider it OK to use two exclamation marks in a single sentence. Any writer should stop to think if even one exclamation mark is needed, as they are overdone. Remember: Their purpose is to convey strong emotion: surprise, excitement, happiness, or even caution or fear.

What does double exclamation mean in email?

Priority. A double exclamation point icon indicates a high-priority message.

Can there be 2 exclamation marks?

More than one exclamation mark doesn't have any meaning. An exclamation doesn't get more "exclamationy" by more marks. Of course, you could still use them, but the interpretation would be entirely up to the reader. Use of punctuation that doesn't have any grounds in grammar would be more like decoration.


1 Answers

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 
like image 132
Brad Gilbert Avatar answered Sep 23 '22 04:09

Brad Gilbert