http://www.perlmonks.org?node_id=1044063


in reply to Conditional array initialisation?

I suppose you want to assign default values to empty arrays only ("doesn't work" ???)

Using defined doesn't make sense here (see also hdb's reply), and the other expression has to be analogous to

DB<125> @a=() DB<126> @a = (@a || ( 1..3 )); => (1, 2, 3) DB<127> @a = (@a || ( 4..6 )); => 3

But the second case fails cause the LHS of an or operator is evaluated in scalar context.

We had a longer discussion about this some weeks ago, which I didn't really follow (maybe somenone can link to it)

FWIW this works:

DB<138> @a=() DB<139> @a = (4..6) if !@a => (4, 5, 6) DB<140> @a=() DB<141> @a = @a ? @a : ( 1..6 ) ; => (1, 2, 3, 4, 5, 6) DB<142> @a = @a ? @a : ( 42 ) ; => (1, 2, 3, 4, 5, 6) DB<143> @a = (4..6) if !@a => "" DB<144> \@a => [1, 2, 3, 4, 5, 6]

Cheers Rolf

( addicted to the Perl Programming Language)