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

hi

my problem is that i have to use hash as a standard interface for communication with some methods, but sometimes my method just checks if something is declared (like a flag) or not (it doesn't have to be initialized (it could but it doesn't have to be))

so my question is how can i make my method understand if i write :

$self->test('database'); and $self->test(database => 'name.db'); sub test { my ($self,---) = @_; my %arg = ---- if (--- eq 'HASH'); this obviously doesn't work, but how to make it work !!! } $self->test(database => 'T'); is something i would like to avoid
any suggestions ?

thnx

Replies are listed 'Best First'.
Re: hash interface
by GrandFather (Saint) on Mar 06, 2009 at 09:52 UTC

    If there is only one parameter then it can't be a named argument list so your code could assume that if it receives more than a single argument then it must be a named parameter list and assign to a hash:

    sub test { my ($self, @args) = @_; my %params; if (@args > 1) { %params = @args; # Will warn if @args has an odd number of ele +ments } else { ... # Handle single argument } }

    However, it is safer and a recommended practise to pass named arguments as a hash reference:

    test ({this => 1, that => 2}); sub test { my ($self, $args) = @_; my %params; if ('HASH' eq ref $args) { %params = %$args; } else { ... # Handle single argument } }

    True laziness is hard work
      However, it is safer and a recommended practice to pass named arguments as a hash reference

      Depends. If you already have references to pass around, ok. If your subroutine / method takes mixed type arguments, ok. But if it is ever only being passed a hash, then constructing a hash at every sub/method call is costly and overkill.

        The cost depends a great deal on what you do with the argument list inside the sub/method. If you don't copy the argument list in some fashion then the hash construction can be expensive for more than a few parameters. However if you would otherwise copy the argument list into an array or hash inside the sub, the cost of constructing the hash is much less relevant. Consider:

        use strict; use warnings; use Benchmark qw(cmpthese); for my $argCount (2, 10, 100) { my @args = (1 .. $argCount); print "\nFor $argCount elements:\n"; cmpthese (-1, { hashArg => sub {noCopy ({@args})}, listArg => sub {noCopy (@args)}, toList => sub {toList (@args)}, toHash => sub {toHash (@args)}, } ); } sub noCopy { } sub toList { my @args = @_; } sub toHash { my %hash = @_; }

        Prints:

        For 2 elements: Rate hashArg toList toHash listArg hashArg 1066864/s -- -12% -27% -74% toList 1205941/s 13% -- -17% -71% toHash 1459027/s 37% 21% -- -64% listArg 4106796/s 285% 241% 181% -- For 10 elements: Rate toList hashArg toHash listArg toList 344656/s -- -14% -17% -91% hashArg 399964/s 16% -- -4% -90% toHash 416260/s 21% 4% -- -89% listArg 3846573/s 1016% 862% 824% -- For 100 elements: Rate hashArg toHash toList listArg hashArg 38850/s -- -6% -7% -98% toHash 41541/s 7% -- -1% -98% toList 41895/s 8% 1% -- -98% listArg 1686904/s 4242% 3961% 3926% --

        True laziness is hard work