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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |