in reply to Re: Is it computationally expensive to return a large string (50-100 kB) from a sub?
in thread Is it computationally expensive to return a large string (50-100 kB) from a sub?
If you want to use a string but not change it OUTSIDE OF THE SUB's SCOPE, pass without references:
(EDIT: See below - you can even change the item without references)
mysub($str);
If you want to update a string during its pass to a sub, pass as reference:
mysub(\$str);
EDIT: From the CB:
<tilly> Here is a function that swaps the first two variables passed in: sub swap {@_[0,1] = @_[1,0];}
<tilly> Perl is pass by reference, return by value, copy by value.
<TedPride> Odd. So as long as you reference via the default input/output array, you can update even if something isn't passed via reference.
<tilly> @_ contains aliases to the passed in values. As soon as you copy it out into local variables you've copied by value. If you call substr directly on the contents of @_ it is faster than having to create and undo references.
So you can even update items that haven't been passed via reference, as long as they stay inside @_. One wonders now why anyone uses references...
EDIT: That should read, "One wonders why anyone uses references when passing data to subs." References are useful for keeping track of anonymous subs, for pointing to sections of nested structures, and so on.
|
---|