Hi,
Can someone please tell me which scenario is the best practice in terms of passing args to a function?
In Scenario 1, foo() calls bar() with a reference to an array that contains only 1 item (that item is a ref to a hash).
In Scenario 2, foo() calls bar() with a reference to a hash array.
Does it make sense to pass in an array that contains only one item (a ref to a hash)? That array will always have only 1 item.
Or is it better to directly pass in the hash array (Scenario 2) ?
Thanks
--Andrew
///////////////////////////////////////////////////////////
#### Scenario 1:
sub foo {
my @arr = ();
my %hash = ();
....
%hash = .... # key/values populated by some API
push(@arr, \%hash);
bar(\@arr, <other_args>); # @arr contains only one item
}
sub bar {
my $array_ref = shift;
.... other function args are accessed here
foreach my $i (.....) {
.... %some_hash initialized here for each iteration of the for
+-loop
push (@$array_ref, \%some_hash);
}
zzz($array_ref); # zzz() will do some read-only operations with
+this array
}
////////////////////////////////////////////////////////////////////////////
#### Scenario 2:
sub foo {
my %hash = ();
....
%hash = .... # key/values populated by some API
bar(\%hash, <other_args>); # <<<----- Is this better/best practi
+ce compared to Scenario 1 ?
}
sub bar {
my $hash_ref = shift;
.... other function args are accessed here
my @array = ();
push (@array, $hash_ref);
foreach my $i (.....) {
.... %some_hash initialized here for each iteration of the for
+-loop
push (@array, \%some_hash);
}
zzz(\@array); # zzz() will do some read-only operations with this
+ array
}
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.