|There's more than one way to do things|
Re: scalar reference and lengthby rjt (Curate)
|on Oct 29, 2019 at 22:53 UTC ( #11108098=note: print w/replies, xml )||Need Help??|
Edit: When I posted this, the question asked if the following code would make a copy of the string:
Everything below the <hr> answers that question. The question has since been edited with much more information, which made my initial answer no longer make (much) sense.
my $s = shift; will not, by itself, increase memory usage, at least on recent Perls:
On my system, that shows an increase only on the last call which modifies the string, mod($string).
This copy-on-write behavior was introduced in Perl 5.20, so results would differ on older Perls, and some specifics will be platform dependent.
There is no size penalty, aside from the insignificant size of the reference itself:
As to whether there is a performance penalty to dereferencing the string, the answer to that is unfortunately yes:
I included two drastically different strings to illustrate the point that Perl does not need to scan the entire string to know the length of the scalar, since the length is part of the internal representation. The performance penalty is for the dereference only.
use strict; use warnings; omitted for brevity.
In Section Seekers of Perl Wisdom