I'm writing a perl subroutine and I would like to have the flexibility to either pass in values as a hash, or just as single values. I would like to know how the arguments are passed to the subroutine, so that I can handle the cases separately. For example:
#case 1, pass in hash
test(arg1 => 'test', arg2 => 'test2');
#case 2, just pass in single values
test('test', 'test2');
sub test {
#if values passed in as a hash, handle one way
if(...) {
}
#if values passed in as single values, do something else
else {
}
}
Is there a way to detect this in perl? Thanks!
What I would do using an anonymous HASH reference :
#case 1, pass in hash
test({arg1 => 'test', arg2 => 'test2'});
#case 2, just pass in single values
test('test', 'test2');
sub test {
my $arg = shift;
if(ref $arg eq 'HASH') {
...;
}
#if values passed in as single values, do something else
else {
...;
}
}
See
http://perldoc.perl.org/perlref.html
http://perldoc.perl.org/perlreftut.html