To do this, pass references to the arrays and dereference in the subroutine:
process_something($scalar1, $scalar2, \@list1, \@list2);
sub process_something
{
my ($scalar1, $scalar2, $ra_list1, $ra_list2) = @_;
my @list1 = @$ra_list1;
my @list2 = @$ra_list2;
}
Another way is to pass all parameters in a hashref:
my $rh_params = {
scalar1 => $scalar1,
scalar2 => $scalar2,
list1 => \@list1,
list2 => \@list2,
};
process_something($rh_params);
sub process_something
{
my $rh_params = shift;
my $scalar1 = $rh_params->{scalar1};
my $scalar2 = $rh_params->{scalar2};
my @list1 = @{$rh_params->{list1}};
my @list2 = @{$rh_params->{list2}};
}
The second method involves more coding but provides a number of benefits:
- No more concerns about getting parameters in the right order
- Any, or all, parameters can be optional with the subroutine setting defaults for missing keys
- Add or remove parameters without changing the subroutine signature
- It also affords a certain degree of data encapsulation
Update: Added missing line: process_something($rh_params);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.