in reply to hash interface
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 } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: hash interface
by shmem (Chancellor) on Mar 06, 2009 at 10:12 UTC | |
by GrandFather (Saint) on Mar 06, 2009 at 10:47 UTC | |
by shmem (Chancellor) on Mar 06, 2009 at 11:14 UTC |