Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This meditation attempts to clarify a fundamental point that many monks have been led astray on. Compare the following two functions:
sub ret_array { return @_; } sub ret_list { return @_[0..$#_]; }
(EDIT: Alkabo pointed out why you shouldn't post code past midnight without re-reading and testing it. I just fixed an obvious typo in ret_list.)

Don't they look similar? Both just return what they were passed, don't they?

But look again. They differ in one significant particular. ret_list returns a list of things. ret_array an array.

But isn't an array a list?

As merlyn put it to me once, a list is a list of things and an array is a bag that contains a list. Now all things in Perl obey a context, which is scalar or list. In list context a list gives you itself. In scalar context it gives the last element. In list context an array gives you the list in the bag. In scalar context it gives you an envelope from the bag, the label saying how many things are in it.

These are seldom the same, and many is the time that a function which was supposed to return a list of words would return a 1 to me unexpectedly when I expected one word.

The moral is this. Think carefully when you write a function that returns a list of things. Some day it will be called in scalar context. Think about what you want it to do when that hapens, then test wantarray() before your return.

In reply to Arrays are not lists by tilly

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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (3)
    As of 2020-02-19 01:46 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (80 votes). Check out past polls.