pap at sotonians.org.uk
Thu Nov 30 05:08:32 CST 2000
Two things Paym,
You need to sign up from your work address if you want to post from
there. southampton-pm has a members only submission thing in action.
Onto your question:-
Anyway here's me spak ladened question my old muckers.
How does perl pass things in functions?
$poo = "Bad shagging data structure";
Whatever oooh the pain.
Now, does perl pass the information as a reference or does it copy the
whole thing. In other words would I make my scripts more efficient by
passing everything by reference?
Over to the perl meisters.
Perl passes things by copy. It has to. As you know, when Perl calls a
function, all arguments are passed as a parameter array.
Perl will create the parameter array as a copy of the original
data. Passing things by reference will allow subroutine modifications to
persist after the scope of the sub.
As for speed, somewhat difficult to say. If you do pass references, you
obviously don't need to return stuff. If you're returning a scalar, Perl
would create a temporary scalar behind the scenes before it arrived in your
return value. If you return an array, Perl will create an temporary array
to shift the stuff into the values you're assigning the result of the
Yadda yadda yadda.
It would seem that by passing references, you'd stop Perl from needing to
create this temporary values. But you'd be wrong. Perl will return
implicitly if it doesn't return explicitly. It'll return the last thing
evaluated by default, e.g. the result of the print function or the if
statement. So whether you return explicitly or not, Perl will still return
something, even if it's delivering the value to void.
Remember, that even if you passed your args as refs, Perl would still
endure the overhead of creating your parameter array.
The /only/ time I use references to function calls is when there's a damn
good reason ( i.e. passing nested structures - or multiple lists or hashes
). If you're after passing scalars, it isn't worth the bother. Remember
that each of your refs is a scalar too, and will cause almost as much overhead.
And I do hope you're using named parameters....
More information about the Southampton-pm