in reply to Re: How to Check whether the Function Parameter is a Scalar or a Hash
in thread How to Check whether the Function Parameter is a Scalar or a Hashref
Another way to explain the same thing (since it took me a minute to parse the above code: I'm sure someone thinks as weirdly as I do!):
sub href_or_scalar { my $param = shift; my ($is_scalar, $is_hashref) = (0,0); if ( ! ref $param ) { $is_scalar = 1; } elsif ( ref $param eq 'HASH' ) { $is_hashref = 1; } else { die("I only accept a scalar or a HASHref!"); } print STDERR "'$param' is: (scalar => $is_scalar, hashref => $is_h +ashref)\n"; } href_or_scalar( 'hello!'); href_or_scalar( 'HASH' ); href_or_scalar( { key => 'value' } ); href_or_scalar( [ 'key' => 'value' ] ); __END__ 'hello!' is: ( scalar => 1, hashref => 0) 'HASH' is: ( scalar => 1, hashref => 0) 'HASH(0x225490)' is: ( scalar => 0, hashref => 1) I only accept a scalar or a HASHref! at c:\test.pl line 12.
|
|---|