lukestyle has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dereference Empty(!) Array
by choroba (Cardinal) on Feb 20, 2012 at 17:54 UTC | |
|
Re: Dereference Empty(!) Array
by ricDeez (Scribe) on Feb 20, 2012 at 21:26 UTC | |
|
Re: Dereference Empty(!) Array
by JavaFan (Canon) on Feb 20, 2012 at 18:19 UTC | |
|
Re: Dereference Empty(!) Array
by Eliya (Vicar) on Feb 20, 2012 at 19:09 UTC | |
|
Re: Dereference Empty(!) Array
by tangent (Parson) on Feb 20, 2012 at 19:09 UTC | |
|
Re: Dereference Empty(!) Array
by Anonymous Monk on Feb 24, 2012 at 21:16 UTC |