In Perl, an object is just a reference to any of the basic Perl data types that has been blessed into a particular class. When you use the ref() function on an unblessed reference, you are told what data type the reference points to. However, when you call ref() on a blessed reference, you are returned the name of the package that reference has been blessed into.
I want to know the actual underlying type of the blessed reference. How can I determine this?
Scalar::Util::reftype()is the cleanest solution. TheScalar::Utilmodule was added to the Perl core in version 5.7 but is available for older versions (5.004 or later) from CPAN.You can also probe with
UNIVERSAL::isa():Obviously, you’d also have to check for
ARRAYandSCALARtypes. The UNIVERSAL module (which serves as the base class for all objects) has been part of the core since Perl 5.003.Another way — easy but a little dirty — is to stringify the reference. Assuming that the class hasn’t overloaded stringification you’ll get back something resembling
Class=HASH(0x1234ABCD), which you can parse to extract the underlying data type: