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

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Memory Use and/or efficiency passing scalars to subs
by Limbic~Region (Chancellor) on Aug 30, 2003 at 17:34 UTC
    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