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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.