Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
The stupid question is the question not asked
 
PerlMonks  

Passing a reference by value is somewhat equivalent to call by reference

by stefp (Vicar)
on Sep 24, 2001 at 11:55 UTC ( [id://114291]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: Is it possible to do pass by reference in Perl?
in thread Is it possible to do pass by reference in Perl?

It is only possible to pass scalars by reference to a sub.

Parameters are passed by value in Perl. But we have an operator, the backslash, that returns reference to any variable (scalar or composite). So the net result is that we have a mechanism to pass parameter by reference.

Admittedly, this not very clean, because the formal parameter is declared as a scalar even if it is a reference to a non scalar. I guess that was the point that tried to convey the sentence: It is only possible to pass scalars by reference to a sub.

sub foo { my $a = shift; push @$a, "foobar"; } my @a = (); foo \@a; # passing a value that is a reference to @a print $a[1]; # prints "foobar"
tilly messages me: another example of how to pass arrays by reference is by using prototypes. Unlike using \ in user code, that is transparent.. So Perl has call by reference after all (even if it was quite a late addition?).

-- stefp

  • Comment on Passing a reference by value is somewhat equivalent to call by reference
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://114291]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.