Remember that, in Perl, your subroutine arguments are an array. If bar will never take any other options, then there is no reason to pass in an arrayref unless you have other uses for the same array and need to avoid building it again, or if you really need for bar to change an array in its caller.

Your scenarios are not quite equivalent. In scenario 1, calling bar changes the value of @arr in foo. In scenario 2, bar constructs its own array rather than changing one of foo's lexicals. In both of these, zzz must be careful not to change its argument, or it will change either @arr in foo (scenario 1) or @array in bar (scenario 2).

Generally, this kind of close coupling is a fertile source of strange bugs and should be avoided unless performance is so critical that avoiding the overhead of building new arrays is needed or passing results back by modifying arguments is absolutely necessary.

This pattern is common in C, but in Perl your return value can also be an array (or something very close to one), so modifying arguments is generally unnecessary and should be avoided (except of course for $self in object methods that specifically exist to mutate an object).


In reply to Re: Best practices for passing in function arguments? by jcb
in thread Best practices for passing in function arguments? by andrewkl

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.