Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Subroutine args by-value vs. by-ref

( #192506=categorized question: print w/ replies, xml ) Need Help??
Contributed by dpenny on Aug 24, 2002 at 01:06 UTC
Q&A  > subroutines


Description:

I'm kind of a newbie, but, I've read all the docs, I've written machine language interfacing to Fortran and C. I've written a simple interpreter in Fortran and again in C. And I stll cannot figure out if Perl passes by value or by reference. An eariler posting talks about "implicit" reference. There is no such thing as implicit at the code level. The docs talk about "turning call-by-reference into call-by-value", hugh, how can this be? Either Perl pushes the value of args (be they scalors or arrays) onto a stack (ie, copies the whole thingy), or it pushes a pointer to the item onto the stack. There is NO OTHER WAY. What is the truth? Why do Perl docs seem so confusing, even uncertain? Can someone explain clearly in a few words? Thanks.

Answer: Subroutine args by-value vs. by-ref
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.

Answer: Subroutine args by-value vs. by-ref
contributed by Anonymous Monk

All your previous experience is confusing you.

Read perldata, perlsyn, perlref and perlsub (if perlsub exists).

There are no pointers in perl, there are only scalar's hashes's and array's, and references. Some might say there are also lists, and they're right.

subroutines get arguments via the magical @_ variable, meaning every time you say Foo(1,2,3), inside of sub Foo, the @_ array will contain 1,2 and 3 as it's value.

That's pass-by-value.

my @Arra = ( 1, 2, 3, 4 ); PassByReference(\@Arra); sub PassByReference { die "@_"; }
Here are some more examples.
use Data::Dumper; DIE(1..10); DIE(1,2,3,4,5,6,,8,9); DIE( [ 1..10 ] ); DIE( { 1..10 } ); my @ARRAY = 1..10; my %HASH = @ARRAY; DIE( @ARRAY ); DIE( %HASH ); DIE( \@ARRAY ); DIE( \%HASH ); DIE( @ARRAY, \%HASH ); DIE( \@ARRAY, \%HASH ); DIE( \@ARRAY, %HASH ); DIE( \&DIE ); ¨ DIE(); sub DIE { print Dumper(@_) };

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (5)
    As of 2014-12-21 19:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (106 votes), past polls