TMTOWTDI:

use Scalar::Util qw(blessed); if (my $class=blessed $foo) { print "Its an object of class $class\n"; } else { print "Its not an object\n"; }

This has the advantage that you can discriminate a blessed reference from an un-blessed one safely, regardless as to what package name it has been blessed into, and avoids the painful filtering of unblessed references which requires overload::StrVal and regexes if you want to be thorough. There is also a function called reftype() which does the converse, telling you what the underlying type is and not what type its blessed into.

#!perl -l use Scalar::Util qw(blessed reftype refaddr); use overload; *isa=*UNIVERSAL::isa; *StrVal=*overload::StrVal; my $array=[]; my $Array=bless [],'ARRAY'; my $hash={}; my $Evil_Hash=bless {},'ARRAY'; foreach my $name (qw($array $Array $hash $Evil_Hash)) { my $ref=eval $name; print "Testing '$name'"; print "ref :",ref $ref; print "U::isa :",isa($ref,'ARRAY'); print "->isa :",eval { $ref->isa('ARRAY') } || do{ chomp($e=$@) +; $e}; print "StrVal :",StrVal($ref); print "blessed :",blessed($ref); print "reftype :",reftype($ref); print "refaddr :",refaddr($ref); print "----" } __END__ Testing '$array' ref :ARRAY U::isa :1 ->isa :Can't call method "isa" on unblessed reference at D:\Temp\bl +essed.pl line 18. StrVal :ARRAY(0x1ab25f0) blessed : reftype :ARRAY refaddr :27993584 ---- Testing '$Array' ref :ARRAY U::isa :1 ->isa :1 StrVal :ARRAY=ARRAY(0x1ab25d8) blessed :ARRAY reftype :ARRAY refaddr :27993560 ---- Testing '$hash' ref :HASH U::isa : ->isa :Can't call method "isa" on unblessed reference at D:\Temp\bl +essed.pl line 18. StrVal :HASH(0x1abf0b8) blessed : reftype :HASH refaddr :28045496 ----
Testing '$Evil_Hash' ref :ARRAY U::isa :1 ->isa :1 StrVal :ARRAY=HASH(0x1abf1a8) blessed :ARRAY reftype :HASH refaddr :28045736 ----

---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

In reply to Re: Asking An Object For It's Class by demerphq
in thread Asking An Object For It's Class by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.