blackjudas has asked for the wisdom of the Perl Monks concerning the following question:

I had a quick chat with jcwren last night on the CB asking if
$fpcr = grep { /fpcr/ } @array ? 1 : 2;
Was a valid statement in logic as it was always returning a value of 0. Now the outcome of the convo is simply the following:
$fpcr = (scalar grep { /fpcr/ } @array) ? 1 : 2;
from what I can gather, scalar is required in front of grep to make grep return a scalar value containing how many matches it made against the list @array. If you drop scalar, it will still be valid since grep returns a result in list form containing all the matches so it would evaluate to true if it returned anything in either fashion. Main question here is, why did adding parentheses around the grep statement make the logic valid, though having the brackets makes it easier to read, I would still assume that perl would interpret the code
grep { /fpcr/ } @array
as a signle statement to test for truth for the ensuing ? 1 : 2 ... Obviously I'm wrong here so can some robed one enlighten me?

BJ

Replies are listed 'Best First'.
(tye)Re: Precedence of grep vs. ?:
by tye (Sage) on Oct 04, 2001 at 21:03 UTC

    See

    $ perl -MO=Deparse $f= grep {/f/} @array ? 1 : 2; <EOF> $f = grep({/f/;} @array ? 1 : 2);
    Note that you have other options beside what you listed. I'd have chosen: $f= grep( /f/, @array ) ? 1 : 2;

    You were on the right track as to the source of your problem with your node title (originally just "precedence"). To quote bits from "perldoc perlop":

    left terms and list operators (leftward) [...] right ?: [...] nonassoc list operators (rightward) [...] Terms and List Operators (Leftward) A TERM has the highest precedence in Perl. They include [...] any function whose arguments are parenthesized. [...] List Operators (Rightward) On the right side of a list operator, it has very low precedence, such that it controls all comma-separated expressions found there. The only operators with lower precedence are the logical operators "and", "or", and "not", which may be used to evaluate calls to list operators without the need for extra parentheses: open HANDLE, "filename" or die "Can't open: $!\n";
    which means that list operators bind tighter than ?: if you use parens but looser if you don't.

            - tye (but my friends call me "Tye")
      Thanks for your help tye! I get it now, I'll have to scan the docs more carefully next time. Though I must admit, sometimes hard to find the exact answer if you're not too sure what you're looking for :).

      BlackJudas
Re: Precedence of grep vs. ?:
by petdance (Parson) on Oct 04, 2001 at 20:59 UTC
    Seems to me that everything to the right of the grep (or rather, the regex that is passed to grep) is evaluated as an expression to be passed in to grep. Without the parens, you're really doing:
    grep( /regex/, @array ? 1 : 2 );
    Randal will disagree, I'm sure, but I always say "when in doubt, use parens".

    xoxo,
    Andy
    --
    <megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

      Randal will disagree, I'm sure, but I always say "when in doubt, use parens".
      Why would you imagine I would disagree with that. That's a line directly from our llama teaching materials.

      Maybe we just disagree on how much is "doubt". {grin}

      -- Randal L. Schwartz, Perl hacker

        Why would you imagine I would disagree with that.

        Because you're also fond of saying "You need to write Perl in Perl", and I suspected that the precedence of such things would be one of those things that One Should Know.

        xoxo,
        Andy
        --
        <megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

Re: Precedence of grep vs. ?:
by Fletch (Bishop) on Oct 04, 2001 at 21:50 UTC

    B::Deparse is your friend in questions of precedence.

    $ perl -MO=Deparse,-p -e '$f = grep /f/, @array ? 1 : 2;' ($f = grep(/f/, (@array ? 1 : 2)));
      Is that part of the distro? I'll go look, but got it from CPAN just in case.

      BlackJudas