in reply to Best practices for passing in function arguments?
G'day andrewkl,
Welcome to the Monastery.
I think you're looking at this back-to-front.
Write your subroutines such that they read the arguments they need. E.g.
sub f { my ($AoH) = @_; for my $hashref (@$AoH) { # do something with $hashref } }
Then call those subroutines with the appropriate arguments. E.g.
... # somewhere in your code f($AoH); ... # elsewhere in your code f([$hashref1, $hashref2]); ... # and at some other point f([\%hash]); ...
As you can see from those examples, you can (often) create the argument list in situ.
What you really want to avoid is subroutine definitions like this:
sub g { my ($arrayref_containing_just_one_hashref) = @_; my $hashref = $arrayref_containing_just_one_hashref->[0]; # do something with $hashref }
So, get the subroutine definition right, then call as needed. If that requires some complex code to create the argument list, so be it.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Best practices for passing in function arguments?
by andrewkl (Novice) on Dec 10, 2015 at 00:01 UTC |