in reply to Re^3: Passing argument by reference (for a scalar)
in thread Passing argument by reference (for a scalar)
using TinyPerl 5.8 under Windows XP
Tinyperl is not fully perl: it doesn't even come with Devel::Peek, which has been core since 5.6, before tinyperl 5.8.0 was released. (I found this out when I vainly tried to use your own favorite pseudo-perl to run ikegami's example, and couldn't, because it didn't have that essential part of core.)
When I use Strawberry Perl 5.8.8 (which comes with all of core), it gets essentially the same results that ikegami already showed: if you use the subroutine's argument directly, rather than making a copy of the string, then Perl doesn't copy the string.
why is it that as soon as I do my $STRING = shift; inside my sub, Perl's memory usage doubles
You do realize that you just essentially asked, "why is it that as soon as I make a copy of the string inside my sub, Perl makes a copy of the string?" Do you understand how silly it sounds when rephrased that way?
Perl 5.8 doesn't have the fancy v5.20 Copy-on-Write ("COW") feature that ikegami mentioned (so the scalar of the copied version doesn't point to the same string buffer), but it does pass the subroutine arguments by reference (so the scalar is the same outside and inside of f($x) ). However, as soon as you make a copy of a string passed by reference, you no longer are using that reference, but a copy of the string -- hence, without COW, the memory goes up when you make the copy , but not when you called the sub . If, like ikegami, you stick to always using the $_[0] rather than creating a copy, it would not double your memory even in tinyperl.
update: rephrased a few things, but essentially meaning has stayed the same
update 2: too harsh in some of my wording, especially the struck out -- sorry.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: Passing argument by reference (for a scalar)
by harangzsolt33 (Deacon) on Sep 10, 2024 at 01:11 UTC |