Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Small troubles with references

by MCS (Monk)
on Feb 09, 2006 at 20:15 UTC ( [id://529180]=note: print w/replies, xml ) Need Help??


in reply to Small troubles with references

The difference is that passing an array to a subroutine passes a copy of that array whereas a reference to an array you can dereference and get the actual array. Perhaps an example will help

#! perl -w use strict; my @array = ("0","1","2","3"); my $aref = \@array; print "Original array: ", @array, "\n"; print "Pass original array to testnonref(): "; testnonref(@array); print @array, "\n"; print "Running sub passing array reference and printing array:"; testref($aref); print @array, "\n"; sub testnonref { my @subarray = shift; $subarray[0] = "4"; } sub testref { my $one = shift; $$one[0] = "4"; }

Here I used your array and array reference (hases work the same way) and first I print out what the array looks like. Then I print out what the array looks like after passing it to the testnonref subroutine. Then I print out what it looks like after passing by reference. Hopefully the output will make it a little clearer:

Original array: 0123
Pass original array to testnonref(): 0123
Running sub passing array reference and printing array:4123

As you can see, passing by reference actually changes the value of your original array whereas just passing the array makes a copymakes a reference to your array using @_ and any changes are lost after the subroutine exits.

Update: thanks, I always assumed (incorrectly) that perl copied the array like other languages. This is why I love perlmonks... always learning better ways of doing things.

Replies are listed 'Best First'.
Re^2: Small troubles with references
by duff (Parson) on Feb 09, 2006 at 20:25 UTC
    The difference is that passing an array to a subroutine passes a copy of that array

    Not true at all. @_ is aliased to all of the elements of the list passed to the subroutine. There is no copying going on unless you, the programmer, do it.

      A lot of newbies are unfamiliar with the distinction between an alias and a reference. And I think we should keep it that way. The fact that an alias is a reference that need not be dereferenced for use is pretty deep and dark voodoo that might confuse a beginner.

      :-)

      ---
      $world=~s/war/peace/g

        I'll bite. I'm not a Perl newbie, but there are certainly holes in my largely self-taught knowledge of Perl. I would guess that the reason the change to the array isn't permanent when it is passed by value is because of lexical scoping ... but the idea of the passed parameter being an alias under the covers is confusing to me. Would anyone care to explain?


        No good deed goes unpunished. -- (attributed to) Oscar Wilde

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://529180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-25 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found