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

Re: Passing references to a sub.

by the_slycer (Chaplain)
on Apr 01, 2002 at 21:42 UTC ( #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
Node Status?
node history
Node Type: note [id://155866]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2020-06-03 10:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (22 votes). Check out past polls.

    Notices?