halfo
halfo

Reputation: 223

Perl array in logical defined-or

I was trying to assign an empty array if a given array is undefined, otherwise assign that given array. But it looks like perl array is being executed in scalar context in logical defined-or.

Example:

@h = 1 .. 5;
@a = @h // ();
@b = defined @h ? @h : ();

print @a; #5  
print @b; #12345

Is there a workaround so I can do in similar fashion like in the second line of the code?

Upvotes: 1

Views: 109

Answers (1)

Borodin
Borodin

Reputation: 126772

Only scalar values can be undef, and it's meaningless to test an array for definedness. If you want to set @a to @h if @h is non-empty, else set it to the empty list, then you can write just @a = @h.

Upvotes: 2

Related Questions