Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
more useful options
 
PerlMonks  

Perl Idioms Explained - !!expr

by broquaint (Abbot)
on Jul 14, 2004 at 12:56 UTC ( #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.

Caveats

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.

Summary

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

Comment on Perl Idioms Explained - !!expr
Select or Download Code
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?

    Thanks

      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.

      HTH

      _________
      broquaint

      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):
      (($expr)?1:'')
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.)

    ihb

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-04-18 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (461 votes), past polls