Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Answer: Is it possible to do pass by reference in Perl?

( #354492=categorized answer: print w/ replies, xml ) Need Help??

Q&A > subroutines > Is it possible to do pass by reference in Perl? contributed by bradcathey

In fact, passing by reference is a must to preserve the values in separate data structures, even an array and scalar. For instance:
my @array = qw ( 1 2 3 4 5 ); my $num = 6; &testsub ( @array, $num ); sub testsub { my ( @list, $single ) = @_; print @list; }
will print:
123456
Passing those values via the @_ flattens all values into one long list. However:
my @array = qw ( 1 2 3 4 5 ); my $num = 6; &testsub ( \@array, $num ); sub testsub { my ( $list, $single ) = @_; print @$list; }
will print:
12345
where passing the array by ref will keep the values from all merging into the single list.

Comment on Answer: Is it possible to do pass by reference in Perl?
Select or Download Code
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 perusing the Monastery: (10)
As of 2014-09-22 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (204 votes), past polls