in reply to Function that accepts both hashes/arrays and hashrefs/arreyrefs
As mentioned by others the risk of sending an array or a hash to a function is risky with lots of unexpected things possible see: perlsub. However if you will limit yourself to only array refs or hash refs then the real question is how to disinguish which one was sent and what to do with it. I like;
Cleaner syntax, Thank you Porculus
Updated code blockmy $Input = $ArrayRef || $HashRef; set_value( $Input ); sub set_value { #recieve the passed variable(s) #my $self = shift;#For OO subs my $Arg = shift; if ( ref $Arg eq 'HASH' ) { ### Handle a hash $Arg->{key} } elsif ( ref $Arg eq 'ARRAY' ) { ### Handle an array $Arg->[$index] } else { ### Must not be an array ref or hash ref } }
Cleaner syntax, Thank you Porculus
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Function that accepts both hashes/arrays and hashrefs/arreyrefs
by Porculus (Hermit) on Jun 19, 2009 at 18:38 UTC |
In Section
Seekers of Perl Wisdom