Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Perl Idioms Explained - !!expr

by broquaint (Abbot)
on Jul 14, 2004 at 12:56 UTC ( [id://374287]=perltutorial: print w/replies, xml ) Need Help??

Perl Idioms Explained - !!expr
sub is_in_list { return !!grep { $_ eq $_[0] } @_[ 1 .. $#_ ]; } print "baz is in list" if is_in_list 'baz', qw/ foo bar baz /; __output__ baz is in list
Even though perl doesn't have a native boolean type, sometimes all a programmer wants is a nice simple boolean value, and this is what the !!expr idiom provides. Given an expression, its resulting value is negated by the boolean not operator ! and then that value is negated to get the resulting boolean value. Let's break that down a little
use Data::Dumper; my $val = 'a string which evaluates to true'; print Dumper !$val; print Dumper !!$val; __output__ $VAR1 = ''; $VAR1 = '1';
So as we can see there the initial negation returns a false value (an empty string '') to signify the negated truthfulness of the value. Then secondary double negation returns a true value (the integer 1) as the negated form of the false value.


Because it is producing a boolean value from an expression it will not preserve any additional information from the expression. But that's about the only caveat, if you could consider it one, and it has the added plus of acting as a boolean internally as explored in diotalevi's Re: Converting to boolean.


Because we are lacking a ¡ operator the !!expr idiom serves to derive a boolean value from an arbitrary expression.

Replies are listed 'Best First'.
Re: Perl Idioms Explained - !!expr
by Anonymous Monk on Jul 14, 2004 at 13:09 UTC

    Can you comment on the alternatives?

    return grep { $_ eq $_[0] } @_[ 1 .. $#_ ]; # or return ~~grep { $_ eq $_[0] } @_[ 1 .. $#_ ]; # or my $searched = shift; return !!grep { $_ eq $earched } @_;

    Should I use them? And if not, why?


      return grep { $_ eq $_[0] } @_[ 1 .. $#_ ];
      Depending on context this will return different things, whereas !!expr will always return a boolean value.
      return ~~grep { $_ eq $_[0] } @_[ 1 .. $#_ ];
      This will return the number of elements returned by grep as opposed to !!expr which, again, will return a boolean value.

      Of course both of the above are valid in appropriate situations, but if you just want a boolean value then !!expr is an appropriate idiomatic solution.



      I like ~~EXPR as a brief (read: obfuscated) way to enforce scalar context on an expression; it only renders the return value unusable if EXPR is a reference.
      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      I would also mention just a simple ternary operator as an alternative:
      return grep($_ eq $_[0], @_[ 1 .. $#_ ]) ? 1 : '';
      or, more generally the equivalent to !!$expr is (some of the parens are optional depending on context):
Re: Perl Idioms Explained - !!expr
by ihb (Deacon) on Jul 20, 2004 at 01:20 UTC

    It should be mentioned that     !!$false numifies to 0 without any warning. (Good thing.)


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://374287]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-06-16 15:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.