Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
It comes down to the difference between lists and arrays. [....] Assigning a list to a scalar gives you the final item in the list.

Oh, yet another falls victim to this seductive lie. It seems to explain so many things after you first "learn" it. Then you start applying it in more and more places and either just start putting out rather nonsense explanations or start dreaming up elaborate schemes about "that isn't really a 'list'". And then you get an emotional attachment to it and start yelling at people for calling things "list" when clearly they mustn't do that because that thing doesn't behave like you think a "list" should in a scalar context. But in the end, it mostly just gets in the way of deeply and accurately understanding the behavior of complex (and some simple) Perl constructs.

The simpler truth is that just about everything in Perl that can return a list can also decide exactly what it wants to return in a scalar context. There is no "list vs. array" dichotomy in Perl. An array returns its size, that much is true. A list literal (aka. "the comma operator") returns its "last 'item'", where the exact definition of "item" belies the conflation of "list" and "list literal".

What qw// returns in a scalar context actually depends on your version of Perl. Some versions of Perl implement qw// as a list literal and so, in those versions of Perl, qw// in a scalar context returns its last item. Other versions of Perl don't and return something else for qw// in a scalar context.

qw// is just another example of a few constructs in Perl where "what should we return in a scalar context?" nobody bothered to design and so what we got was an accident of implementation details (or even optimizations).

It seems to me that putting qw// into a scalar context is most likely to indicate a bug and so I wouldn't be surprised if a future version of Perl simply makes that fatal (or just a warning).

Indeed, it is common for many non-array things in Perl to return "the last 'item'" in a scalar context (where the definition of 'item' is somewhat slippery, if you are paying close attention). And it is common to talk about many such non-array things as being "a list". But not all "lists" in Perl return their last item in a scalar context. And arrays actually are lists.

The real "difference between lists and arrays" is that an array is a list that is stored in a variable (named or anonymous) and so can persist longer and allow more operations (like pop). The answer to "What to return in scalar context?" is a much more fine-grained than "is it an array or not?", despite it not appearing so when you first start looking.

- tye        

In reply to Re^2: Trouble getting size of list returned from sub ("list") by tye
in thread Trouble getting size of list returned from sub by wanna_code_perl

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2018-05-26 04:36 GMT
    Find Nodes?
      Voting Booth?