in reply to Re: Overloading ref() just like ->isa and ->can - best practice for isa(HASH)?
in thread Overloading ref() just like ->isa and ->can?
If you want to know whether it really, truely is a hash, see if SVt_PVHV is true for the SV. See sv.h for the dirt.
use B qw( svref_2object SVt_PVHV ); sub is_hash { my $ref = shift @_; return ref( $ref ) and svref_2object( $ref ) & SVt_PVHV; }
If you just want to know whether it supports being treated like a hash, do something akin to a ->can test for hash-ness. If the hash dereference doesn't die, then $ref acts like a hash.
sub does_hash { my $ref = shift @_; return ref( $ref ) and eval { %$ref; 1 }; }
⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊
|
|---|