Don't ask to ask, just ask | |
PerlMonks |
Re: perl string pass by valueby davido (Cardinal) |
on Apr 02, 2018 at 14:44 UTC ( [id://1212167]=note: print w/replies, xml ) | Need Help?? |
It's worth noting that the special variable, "@_" contains aliases to the calling args, not copies of them. Therefore, there is no copy being made here:
...because we're acting only on the aliased entity. Similarly, there should be no copy here (assuming the s/// operator doesn't make one):
No copy because we acted upon the alised entity, which is the same as acting upon the entity itself. On the other hand, had we unpacked our args by assigning $string to some other variable lexically scoped to the subroutine, done a substitution (probably triggering copy on write), and then returned the string, we have the opportunity to have created at very least one copy. Copying small strings and simple values such as integers, and even floating point numbers is pretty fast, and for general cases we probably shouldn't care. But for long strings I can see where it would be useful to be aware of what will and will not trigger a buffer copy. On the other hand, consider this:
Dave
In Section
Seekers of Perl Wisdom
|
|