Ok so maybe not exactly the correct title, but i'm flummoxed why the first two work as expected, but the third results in an empty array:
1) sub ding { @foo = @{$_[0]}; }
2) sub ding { @bar = @{$_[0] || ()}; }
3) sub ding { @jazz = @{$_[0]} || (); }
Thanks very much,
Luke
update:
I was just interested in comparing why it is that i can copy an array and initialise arraycopy as empty in case array is undefined:
4) @arraycopy = @array || ();
But that i'm not able to follow the same pattern when dereferencing the arrayref in 3)
Would anyone ever do 2), or would they be more verbose?
2b) @bar = defined @{$_[0]} ? @{$_[0]} : ();
update 2:
Well this is embarrasing, Re @choroba: "The || operator forces scalar context on the left operand". I was having one of those crazy moments where I was convinced 4) was what I always did(!) And Then trying to figure why applying the same principal to an array ref in 3) didn't work. I guess I was thinking of (re)initialising an empty array as in simply @array = ();
But forgetting my original question, Re @Eliya: "my @foo = ref $_[0] eq "ARRAY" ? @{$_[0]} : ();" this is better than 2b) and I've learned more to boot. I promise too next time I'll try to explain the boundaries of my question better.
In reply to Dereference Empty(!) Array by lukestyle
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |