in reply to Confused by Perl ternary operator

You should be writing
$param2 = @_ ? 'Shift' : shift;
instead of
@_ ? $param2 = 'Shift' : $param2 = shift;
But if you want to do what you're doing, you need some parentheses.
@_ ? $param3 = "Shift" : ($param3 = shift);
I don't know what the parsing problem is, exactly, but no doubt someone else has already posted it while I've been composing this. :)

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Confused by Perl ternary operator
by diotalevi (Canon) on Aug 23, 2004 at 19:23 UTC

    In this case, for arcane reasons, $param3 = @_ ? "Shift" : shift works as-is but is execution-order unsafe and if the expression were included in some other larger expression, could be quite a problem.

    $param3 = @_ ? "Shift" : $_[0]; shift;

    The preceding example would be the safe way to write it.

      How is that execution-order unsafe? ?:'s first operand has to be evaluated before either the second or third.

      After writing the above, I checked perlop.pod, and technically you are correct. ?: isn't documented as short-circuiting, so perl potentially could evaluate the 2nd and 3rd operands before testing the first one. However, I believe that it is intended to short-circuit and will submit a patch to the doc.

      As an interesting side note, structures with else or elsif are implemented using condexpr (the ?: operator). That is,

      if (foo) { bar } else { baz }
      is identical to
      foo ? do { bar } : do { baz }
      so the short-circuiting of ?: is pretty guaranteed.

        I was thinking that someone might use that expression that reads and writes to @_ inside another expression that depends on whether @_ has already been fetched or altered. ?: isn't just if/else, its an expression which can be used inside other expressions.

        $join = ( @_ ? "Argument, the first" : shift ) . ( @_ ? "Argument, the second" : shift );
Re^2: Confused by Perl ternary operator
by Anonymous Monk on Aug 24, 2004 at 13:55 UTC
    C:>perl -MO=Deparse,-p -e"@_ ? $param2 = 'Shift' : $param2 = shift;" ((@_ ? ($param2 = 'Shift') : $param2) = shift(@ARGV)); -e syntax OK ( ( @_ ? ( $param2 = 'Shift') : $param2 ) = shift(@ARGV) );