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

knexus,
It would be a good way to handle the problem, but I don't think you are doing it quite right using conventional syntax.
my $large_scalar = 'a huge string like 64K or something'; modify_largescalar(\$large_scalar); sub modify_largescalar { my $scalar_ref = shift; # Code to modify $$scalar_ref; }
Your use of prototypes isn't helping (but maybe you need that for some other reason). The thing is you are not creating refs until you are inside the sub - which means you are already copying the large scalar. You are using the fact that the elements of @_ are aliases to the variables and avoiding copying by assigning new variable names to references to the aliases. I do not know if this will avoid copying, but it certainly isn't conventional syntax. I also wouldn't worry about optimization of these large scalars unless they are truly large and/or you are calling these subs many times in a tight loop.

Hope this helps - L~R

Updated after CombatSquirrel pointed something out I took another look at perldoc perlsub and conferred with belg4mit in the CB.

Replies are listed 'Best First'.
Re: Re: Memory Use and/or efficiency passing scalars to subs
by CombatSquirrel (Hermit) on Aug 30, 2003 at 15:23 UTC
    Limbic~Region, are you sure of that? I thought this at first too and wrote a piece of code to demonstrate it:
    sub direct(\$\$) { print $_[0] . " " . $_[1] . "\n"; } sub indirect($$) { my ($one, $two) = \(@_); print $one . " " . $two . "\n"; } my ($first, $second) = qw(Hi there); print \$first . " " . \$second . "\n"; direct $first, $second; indirect $first, $second; __END__ SCALAR(0x1823c78) SCALAR(0x224f88) SCALAR(0x1823c78) SCALAR(0x224f88) SCALAR(0x1823c78) SCALAR(0x224f88)
    But the scalar refs point to the same memory address, which is natural for the main program and the direct sub, but completely unexpected (at least for me) for the indirect sub.
    Maybe there is a different explanation for this behaviour, though.
    Cheers,
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
      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

        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