...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.


In reply to (Ovid) Re: Most Efficient by Ovid
in thread Most Efficient by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.