in reply to Ternary operator can't be used as first argument of push
It is true that the trinary operator can be used as an lvalue. On the other hand, I can't think of a legitimate reason why one would want to do so (although I'm open to suggestions).
As a first approximation, I think the expression is better written as
if( $flag ) { $on = $value; } else { $off = $value; }
Then again, the repeated assignments of the same value does look a little redundant. This could be recast as
${$flag ? \$on : \$off} = $value;
Which, not coincidentally, looks remarkably similar to the problem of pushing to the correct array, and to my twisted mind, seems a bit easier to follow than the initial code. The fact that the code looks weird alerts you to the fact that something weird is indeed going on.
But I digress. I posited that one should never use the ternary operator as an lvalue. When I see code that does this, it is usually a degenerate (in more ways than one) form of a hash. I would probably recast the two scalars as keys of a hash, which would give something like:
$status{$flag ? 'on' : 'off'} = $value;
That, to me, looks the clearest of all.
|
|---|