On Fri, 8 Jun 2001, David M. Lloyd wrote:
> I want to check to see if an argument is a reference to a hash.
>
> Is this the best way to do it:
>
> if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) {
> /* It's a hash ref */
> } else {
> /* It's not */
> }
Looks good - for comparison, a snippet of code that was in production
somewhere where I would have been beaten severely about the head with large
medieval objects if it hadn't worked:
/* is it a reference */
if (SvROK(*svpp))
{
/* what kind of reference is it? */
svr_type = SvTYPE(SvRV(*svpp));
if (svr_type != SVt_PVHV)
{
/* PROBLEM : Not a hash reference */
Might want to state it as
if (SvROK(arg) && (SvTYPE(SvRV(arg)) == SVt_PVHV) )
Or even #define a macro:
#define PERL_HASH_REF(a) (SvROK(a) && (SvTYPE(SvRV(a)) == SVt_PVHV) )
But I guess that's a matter of taste...
--
Vivek