<p>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.  </p>

<p>I want to know the actual underlying type of the blessed reference.  How can I determine this?</p>

[{'answer_id': 42237, 'author': 'Jagmal', 'author_id': 4406, 'author_profile': 'https://Stackoverflow.com/users/4406', 'pm_score': 2, 'selected': False, 'text': '<p>And my first thought on this was: "Objects in Perl are always hash refs, so what the hack?"</p>\n\n<p>But, Scalar::Util::reftype is the answer. Thanks for putting the question here.</p>\n\n<p>Here is a code snippet to prove this.. (in case it is of any use to anyone).</p>\n\n<pre>\n$> perl -e \'use strict; use warnings "all";\n            my $x = [1]; bless ($x, "ABC::Def");\n            use Data::Dumper; print Dumper $x;\n            print ref($x) . "\\n";\n            use Scalar::Util "reftype"; print reftype($x) . "\\n"\'`\n</pre>\n\n<p>Output:</p>\n\n<pre>\n$VAR1 = bless( [\n                 1\n               ], \'ABC::Def\' );\nABC::Def\nARRAY\n</pre>\n'}, {'answer_id': 45629, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 3, 'selected': False, 'text': "<p>You probably shouldn't do this. The underlying type of an object is an implementation detail you shouldn't mess with. Why would you want to know this?</p>\n"}, {'answer_id': 64160, 'author': 'Michael Carman', 'author_id': 8233, 'author_profile': 'https://Stackoverflow.com/users/8233', 'pm_score': 5, 'selected': True, 'text': '<p><code>Scalar::Util::reftype()</code> is the cleanest solution. The <a href="http://search.cpan.org/dist/Scalar-List-Utils/lib/Scalar/Util.pm" rel="noreferrer"><code>Scalar::Util</code></a> module was added to the Perl core in version 5.7 but is available for older versions (5.004 or later) from CPAN.</p>\n\n<p>You can also probe with <code>UNIVERSAL::isa()</code>:</p>\n\n<pre><code>$x-&gt;isa(\'HASH\')             # if $x is known to be an object\nUNIVERSAL::isa($x, \'HASH\')  # if $x might not be an object or reference\n</code></pre>\n\n<p>Obviously, you\'d also have to check for <code>ARRAY</code> and <code>SCALAR</code> types. The UNIVERSAL module (which serves as the base class for all objects) has been part of the core since Perl 5.003.</p>\n\n<p>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 <code>Class=HASH(0x1234ABCD)</code>, which you can parse to extract the underlying data type:</p>\n\n<pre><code>my $type = ($object =~ /=(.+)\\(0x[0-9a-f]+\\)$/i);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/11085', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/166/']