in reply to Re^4: Adding attributes to values?
in thread Adding attributes to values?
sub get_args { if (scalar(@_) == 1) { if (ref($_[0]) eq 'HASH') { return "got named args!\n"; } elsif (ref($_[0]) eq 'ARRAY') { return "got positional args\n"; } else { die "argument wasn't a hash or array reference!\n"; } } else { die "got more than one arg!\n"; } } sub named_or_positional { print get_args(@_); } named_or_positional({foo => 'avalue' , bar => ['a', 'b']}); named_or_positional(['whatever' , 'we', 'are', 'passing']); named_or_positional('whatever' , 'we', 'are', 'passing');
Then you would have to check whether there is a single hash_ref or a single array_ref for determining whether it's positional or named. Any other case than a single hash or array ref would have to be considered a error.
If you don't mind the additional overhead of square/curled brackets, then it would allow you simple differentiation of named an positional args.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Adding attributes to values?
by oyse (Monk) on Oct 21, 2007 at 11:15 UTC |