in reply to Most Efficient

...I was wondering whether someone could enlighten as to which method is more efficient.
What do you mean by "efficient"? Do you mean programmer efficiency, runtime efficiency, ease of maintenance, etc? There are many considerations regarding efficiency. One answer is not enough.

  1. Your first method, a straight subroutine in a package, is likely to be the easiest to code and the fastest to execute. However, it may face significant maintenance difficulties as time goes on.
  2. The object oriented method is more work to code, runs a bit slower, but tends to easy maintenance. Once the object is created (with a standardized interface), it's simple and easy to use and extend.
I'm wondering which is the method of choice, especially if I start passing roughly 6 params to packaged subroutines.
If you're unsure of the number of params to pass, or if it's likely to change, consider using a hash or hash reference as the argument to the sub.

Consider:

my $url = createReturnURL ( session => $session, host => $host, params => \@params );
This will flatten to a list. The actual sub looks like this:
sub createReturnURL { my %argument = @_; if ( exists $argument{ session } ) { # do something } # more code here }
With the arguments being passed as a hash, you are no longer forced to worry about the order of the arguments, and assigning default arguments in the actual subroutine is trivial. Further, extending the functionality of this subroutine is as simple as adding a new hash key and testing for its existence within the subroutine.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.