Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have two choices with which to implement passing data to subroutines in packages and I was wondering whether someone could enlighten as to which method is more efficient.

The first method uses the standard method of passing values to subroutines:
package Temp; sub test { my ($params)=@_; print "$params"; } ###main code use Temp; Temp->test("some param");
The other method uses perl oo to use the package's constructor to store the variable values.
package Temp; sub new { my ($class,$params)=@_; my $self={}; $self->{param}="$params"; bless($self,$class); return $self; } sub test { my ($self)=@_; my $param=$self->{param}; print "$param"; } ###main code use Temp; my $obj=Send->new("some param"); $obj->test();
Aside from a syntaxical errors, both of these methods should and do produce the same results. I'm wondering which is the method of choice, especially if I start passing roughly 6 params to packaged subroutines.
Thanks.

Replies are listed 'Best First'.
(Ovid) Re: Most Efficient
by Ovid (Cardinal) on Jan 16, 2001 at 02:44 UTC
    ...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.

Re: Most Efficient
by gildir (Pilgrim) on Jan 16, 2001 at 17:42 UTC
    This is more a design question than programming one. Important fact here is the relation of 'param' to class Temp.

    If 'param' is a natural part of class Temp, that second (aggregation) example is much better. Value of 'param' can now be used by all methods of object 'Temp'. This scenario is usualy used in container-element relation, like relation between object 'User' and object 'Address'. Address is a natural part of user description.

    If 'param' is just a parameter that influence execution of method test(), and test is the only method that is influenced by param, that the first example is better. For example if you have method auth() in object User, this method could have parameter '$password' that should be compared to password stored in object User and returns true or false value. There is no need for $password parameter to be used by other methods but auth().