Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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 lurking in the Monastery: (4)
As of 2014-07-12 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls