@array1 = get_array2() || ();

The problem with this statement is that the  || (logical-or) operator causes  get_array2() to be invoked in scalar context. So, of course, evaluation of  @array2 in the return statement of the function is in scalar context and you get the number of elements of the array as the returned value.

There may be a conceptual disjunction here: while a scalar can be undefined or hold an empty string (or a few other things), an array only ever holds zero or more scalar elements. There is no such thing as an "undefined" array. (Update: Of course, an array element, if any exist, is always a scalar, so you could have an array with one or more undefined elements.)

Update: Take a look at Context tutorial in the PerlMonks Tutorials.

Update 2: Another conceptual problem with the quoted statement is rooted in the fact that a function only returns a list of zero or more elements. So what the statement is saying (or trying to say) is "assign the return list of the function call to the array unless the list is empty, in which case assign the literal empty list," which kinda doesn't make a lot of sense.

Update 3: Changed "zero or more elements" to "zero or more scalar elements" in second paragraph above just for sake of clarity.


Give a man a fish:  <%-{-{-{-<


In reply to Re: Return array from sub or return empty array by AnomalousMonk
in thread Return array from sub or return empty array by gregzartman

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.