Re^4: Passing argument by reference (for a scalar)
by pryrt (Abbot) on Sep 09, 2024 at 21:41 UTC
|
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.
| [reply] [d/l] [select] |
|
|
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.
Oh, Okay! I think, I understand it now. Thank you for explaining it at my level. Lol
| [reply] |
Re^4: Passing argument by reference (for a scalar)
by ikegami (Patriarch) on Sep 09, 2024 at 21:26 UTC
|
Same thing would happen if you did my $STRING = ${ +shift }; if you had passed a reference. If you don't want a copy, don't make a copy! (Also, don't use a 20 year old version of the language!) This was already all covered in detail. Including how what you claim happens hasn't been true for 10 years.
| [reply] [d/l] |
|
|
I'm not familiar with ${ +shift }; syntax. It looks like the + is a way to interpret 'shift' as a command inside ${}. I'm not readily finding documentation for this. Where should I look?
| [reply] [d/l] [select] |
|
|
Look at the bottom of Not so symbolic references in perlref.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] |
|
|
|
|
| [reply] [d/l] |
|
|
+5 # Same as `5`
+"abc" # Same as `"abc"`
+@a # Same as `@a`, in both scalar and list context
${ shift } is just a weird undocumented way of writing $shift. I didn't want that. I wanted to use the shift operator. ${ +shift } can't be parsed as a scalar named +shift, so it has the desired effect.
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|