in reply to Re: font color="#ff0000"IF NOT! IF NOT!/font
in thread IF NOT! IF NOT!

As an aside, the construct ! grep EXPR, LIST is used in a boolean context. Either grep returns a list or it doesn't. ! evaluates in a boolean context. A list is TRUE and no list is FALSE.

I've used it a lot in extended EXPR's, so that I don't have to work through the negation of my && or || that sending if to unless (or vice-versa) would do.

--------
/me wants to be the brightest bulb in the chandelier!

Replies are listed 'Best First'.
Re3: IF NOT! IF NOT!
by Hofmator (Curate) on Aug 03, 2001 at 13:11 UTC

    As an aside, the construct ! grep EXPR, LIST is used in a boolean context. Either grep returns a list or it doesn't. ! evaluates in a boolean context. A list is TRUE and no list is FALSE.
    Not the best way to put it, IMHO ... to clarify: in scalar context grep returns the number of times it matched. So if it didn't match anything, it returns 0 which evaluates to false. Otherwise it returns a positive number which evaluates to true.

    The boolean context is enforced by the if (and by the !), so that the grep in if (grep /blah/, @array)also evaluates in boolean context.

    -- Hofmator

      Is it that grep returns a number when evaluated in a scalar context or that grep returns a list which, when evaluated in scalar context, gives the number of matches?

      I know it sounds like a semantic difference, but I'm just curious.

      ------
      /me wants to be the brightest bulb in the chandelier!

        says perldoc -f grep

        In scalar context, returns the number of times the expression was true.
        And watch your language ;) a list evaluated in scalar context does not give its length my $len = (3,4,5); # $len == 5 ! only an array gives back its length in scalar context.

        It would be very inefficient to first build up an array with lots of elements and then only use its length. That's why only a number is returned directly.

        -- Hofmator

Re: Re: Re: font color="#ff0000"IF NOT! IF NOT!/font
by suaveant (Parson) on Aug 02, 2001 at 22:59 UTC
    Ahhh... I wasn't sure... the code had me a bit confused anyway... backwards logic does that :)

                    - Ant