in reply to Re: Re: Memory Use and/or efficiency passing scalars to subs
in thread Memory Use and/or efficiency passing scalars to subs

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Memory Use and/or efficiency passing scalars to subs
by knexus (Hermit) on Aug 30, 2003 at 17:21 UTC
    Thanks for all the insight, it has been helpful. Being new to perl and not programming, I am probably making more out of things than I need to at this point. ;-)

    Anyhow, I discoved that the only sure way of not making a copy that I know of is to use $_[0],$_[1], directly in the subroutines.

    For me the problem became one of readability when I had 3 or more passed references and a fair amount of code in a single sub. So, I tried to find a way to use named vars instead of the $_[0], $_[x] approach which was getting confusing and leading to stupid bugs.

    Any suggestions on a good way to accomplish that would be appreciated.

    BTW: The code I listed in the original approach does not appear to make a copy of the scalars, but I can honestly say I am not qualified at this point to say its a fact. :-) Thanks

      knexus,
      The code that perrin and I provided does exactly what you want. First pass a reference to the scalar to the sub instead of the scalar itself.
      modify_large_scalar(\$large_scalar);
      Notice the \ in front of the $large_scalar. This makes an explicit reference to the variable. Then inside the sub, shift off the reference into a properly named variable.
      sub modify_large_scalar { my $large_scalar_ref = shift; # Code to modify $$large_scalar_ref }
      I have been following this thread and you said that it was taking up to 13 hours to run. Chances are there are other ways to improve the efficiency of the code. You might want to post all of your code (inside readmore tags if it is long) to see if we can't come up with some more efficient ways of accomplishing the same thing. Additionally, you should look into modules like Benchmark and Devel::Profile to see if the problem is where you think it is.

      Cheers - L~R