CombatSquirrel,
My understanding of aliasing was wrong, so my comments that have been striked are probably incorrect as you pointed out - though my syntax is more conventional. Read on if you are interested or take a look at perldoc perlsub.

The @_ array in a sub is like what happens in a foreach loop where modifying $_ during an iteration is modifying the actual element itself. This is accomplished without copying the array (in current versions of Perl), but with some internal stuff that creates aliases.

Something similar happens in a sub. Each element in the @_ array is an alias to the actual variable - so you can modify it and change the variable it aliases. According to belg4mit in the CB, copying doesn't actually happen until you make an assignment such as my $variable = $_[0];. The thing is knexus didn't do that - they made an assignment to a reference to the alias. I do not know if that makes a copy or not (Benchmark would be one way to find out for sure).

In any case I stand by my original post for a more conventional way of doing it even if my rationale was flawed.

Cheers - L~R


In reply to Re: Re: Re: Memory Use and/or efficiency passing scalars to subs by Limbic~Region
in thread Memory Use and/or efficiency passing scalars to subs by knexus

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.