[ACCEPTED]-What does !! (double exclamation point) mean?-negation

Accepted answer
Score: 53

It is just two ! boolean not operators sitting 8 next to each other.

The reason to use this 7 idiom is to make sure that you receive a 6 1 or a 0. Actually it returns an empty string 5 which numifys to 0. It's usually only used 4 in numeric, or boolean context though.

You 3 will often see this in Code Golf competitions, because 2 it is shorter than using the ternary ? : operator 1 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
Score: 12

not-not.

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

Score: 0

Because three other answers claim that the 4 range is "0" or "1", I just thought I'd 3 mention that booleans in Perl (as returned 2 by operators like ==, not, and so on) are undef and 1 1, not 0 and 1.

More Related questions