In C and C++, the default mechanism for passing arguments to a function is pass-by-value. To get pass-by-reference, you need to (in effect) override the default by explicitly either (1) passing a pointer (simulated pass-by-reference) or (2) declaring the function parameter to be a reference (C++ only).

In Perl, it’s the other way around. The default mechanism for passing arguments to a subroutine is pass-by-reference. For example, if you call:

foo($x, $y, $z);

then within the body of sub foo the special array variable @_ contains references to the three arguments. In Perl terminology, $_[0] is an alias for $x, $_[1] is an alias for $y, and $_[2] is an alias for $z. This is strictly equivalent to a reference in C++: any changes made to $_[0] within sub foo are actually made to $x. The normal Perl idiom is to simulate pass-by-value by beginning the function body like this:

sub foo { my ($p, $q, $r) = @_; ... }

which makes local copies of the subroutine arguments.

Note that in Perl arguments are passed in to @_ as a flat list of scalar values. So if you want to pass in, say, two arrays, and retain their separate identities, you use references: foo(\@array1, \@array2); which are themselves scalars. These work like C pointers (or C++ references). As hippo has explained, Perl objects are also references, so when an object argument is copied within a subroutine, it is only the reference — and not the underlying object — which is copied.

As regards return-by-reference, remember that Perl uses reference-counted garbage collection, so it is safe to create an object within a subroutine and then return it to the caller. And since objects are references, returning an object involves the copying of a single scalar value (the reference) only.

See perlreftut and perlootut.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: pass by value vs reference and return by value by Athanasius
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.