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.

- tye        


In reply to Re: pass by value vs reference and return by value (terms) by tye
in thread pass by value vs reference and return by value by ganeshPerlStarter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.