Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Answer: Subroutine args by-value vs. by-ref

by wog (Curate)
on Aug 24, 2002 at 06:02 UTC ( #192525=categorized answer: print w/replies, xml ) Need Help??

Q&A > subroutines > Subroutine args by-value vs. by-ref - Answer contributed by wog

The implict reference is not a hard reference, as perlref speaks of, but an aliasing that occurs. For example:

sub x { $_[0] = "in x"; } my $foo = "outside"; x($foo);
... will set $foo to "in x". The thing in @_ has become an alias for $foo. However, you won't get this behavior if you explictly copy the value out of @_, as in:
sub x { my($foo) = @_; $foo = "in x"; } my $foo = "outside"; x($foo);
When you copy the value, you are not working with an alias to the thing passed in, but you a working with a copy, bringing you the pass-by-value semantics that are probably more intutitive to you.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2017-01-20 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (177 votes). Check out past polls.