in reply to faulty expression prob

This should work:

return $pkg->{ DIST } = ( $rh = $pkg->{ RPM_HDR } and $dist = $rh->tag +( DISTRIBUTION ) ) ? $dist : $undef;

Replies are listed 'Best First'.
Re^2: faulty expression prob
by perl-diddler (Chaplain) on Apr 03, 2008 at 23:07 UTC
    That's were I was going (where I wanted to end up) as I was trying to remove the duplication in the code. The 2 mentions of the L-value "$pkg->{DIST} were definite candidates for reduction, but I got stuck along the way when the less 'reduced' expression I initially posted wasn't working as expected...Perhaps someone can point out the "logical error" from the following change of "logic" (I'm paying careful attention to op-precedence as others have suggested that might be the problem). BTW, "?:" has higher precedence than assignment "=". "and" is lower than both. From the orig (dropping unnecessary parens around assign before 'and' (numbering for greater easy in referring back to which step is causing problem) (0):
    return ( $rh=$pkg->{'RPM_HDR'} and $dist=$rh->tag('DISTRIBUTION') ) ? $pkg->{'DIST'}=$dist : $pkg->{'DIST'}=$undef;
    Logically substituting "X" for the conditional expression in parens, "( $rh=$pkg->{'RPM_HDR'} and $dist=$rh->tag('DISTRIBUTION') )", I get (1):
    return X ? $pkg->{'DIST'}=$dist : $pkg->{'DIST'}=$undef;
    AHAH!....I see the prob...that "=" on the "end" (before $undef)... It is lower precedence than the ?: (as mentioned above...but wasn't sure which '=' the '?:' was interfering with). So the above (using D for "$pkg->{'DIST'}) is really (2?):
    return (X ? D=$dist : D ) = $undef;
    Um...Why wouldn't that be a syntax error? the "(X?D=$dist:D)" part isn't a valid address that can be assigned to. (Note. "$undef" != undef, it's equal to a string of length 5 characters, 'undef'). I can see why what I wrote would be wrong -- but why wouldn't Perl flag it as illegal syntax?
      Why wouldn't that be a syntax error?

      Because ?: can be used on either the right hand side of an assignment:

      my $var = $test ? $value_one : $value_two;

      Or on the left hand side of an assignment:

      ( $test ? $var_one : $var_two ) = $value;

      And because $pkg->{'DIST'} can be assigned to it is not a syntax error.