Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Passing references to a sub.

by the_slycer (Chaplain)
on Apr 01, 2002 at 21:42 UTC ( [id://155866]=note: print w/replies, xml ) Need Help??


in reply to Passing references to a sub.

The other use of references is when you need to pass two arrays (or a hash and an array) into a sub.
Consider:
my @array1 = ("foo", "foo1", "foo2"); my @array2 = ("bar", "bar1", "bar2"); print_data(@array1, @array2); sub print_data { @array1 = @_; #array1 gets all of @_ @array2 = @_; #array2 also gets all of @_ }
vs
print_data(\@array1, \@array2); sub print_data { my @data1 = @{$_[0]}; #dereferences first array into @data1 my @data2 = @{$_[1]}; #dereferences second array into @data2 }
The second version is more often than not what you actually want.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-18 04:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found