Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "?:" operator cannot return list?

Tags:

perl

perlop

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;

like image 321
Eugen Konkov Avatar asked Jul 28 '26 02:07

Eugen Konkov


1 Answers

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.

like image 191
Richard RP Avatar answered Aug 01 '26 14:08

Richard RP