perlperlop

Why "?:" operator cannot return list?


Why ?: operator can not return list?

my $hash =  {
    ...
    ($row->active?checked=>1:()),
};

The DOC say nothing about scalar or list context

UPD
Another example:

@list =  2,3;         # CORRECT
@list =  1? 2,3 : (); # Syntax error

Why first is OK, but second is not? It seems there should not be the problem for perl to just propagate 2,3 to the outer context;


Solution

  • The problem is that , and => (the list separators) have lower precedence than ?: and =.

    So it's not a question about whether perl is passing the right hand side of = as a list or scalar. It's a syntax error because @list=1?2 and 3:() are handled as separate items of a list, each containing half a ?: statement which is not allowed.