|The stupid question is the question not asked|
Re: pass by value vs reference and return by value (terms)by tye (Cardinal)
|on Oct 27, 2013 at 16:46 UTC||Need Help??|
In C++ "reference" means a pointer that is automatically dereferenced (so the syntax of using it is the same as the syntax for a non-pointer but the underlying mechanics are the same as a pointer). In Perl, "reference" just means "pointer" (there are, of course, differences between a Perl reference and a C++ pointer, but they are trivial differences compared to those between a Perl reference and a C++ reference).
In Perl, even at the point where you declare/create an object, you are dealing with a Perl reference (a pointer). So passing in or returning an object in Perl never involves copying the object (just copying the pointer). So your concerns simply don't apply to Perl objects.
So the answer to your question is: It doesn't matter. You already aren't making copies of objects so stop worrying about that. But I'd like to address a few more confusing points.
So, given that "reference" means something quite different in Perl, people who say that Perl does "pass by reference" are incorrect. What Perl does (always, not "by default") is "pass by alias". If you directly access individual members of @_, then you are accessing the actual variables given as arguments to the function (for each argument that was simply a variable, not some expression). This is similar to what other languages call "pass by reference".
The best and common practice is to copy the aliases from @_ into named lexical variables at the very top of the function so that the effect is "pass by value". The copying can be seen explicitly in the code; it isn't some option with how you declare the function.
But, of course, with Perl objects, copying the lexical value is just copying the reference (a pointer), so both copies still point to the same object. So changes made through one copy will be seen by the other copy. If you want a function to make changes to a copy of an object and not have those impact the original object, then you have to do the copying explicitly, probably by calling a method on the object. But there is no one standard method name for copying an object in Perl. Sometimes $object->new() does that. Other times the method is called "clone". Many classes have no copy method implemented.