Why are you asking for an object's class? Typically a person needs this when they're checking whether an object is of a certain kind so they can either know what it's capable of or to somehow make a decision. If that's your purpose then you've asked for the wrong piece of information. If you want to know whether an object is of a particular kind, use $obj->isa( $your_class ). That will respect any normal object oriented ISA relationships. If you want to know whether an object is capable of doing something you can use the $obj->can( $method_name ) to ask whether it has a method of a particular name. You'll find these documented in the normal documentation like perlobj.

if ( ref( $obj ) eq 'Foo::Bar' ) { # This is generally wrong } use Scalar::Util 'blessed'; if ( blessed( $obj ) and $obj->can( $some_method ) ) { # We asked if the object could do something. We win. OO inheritanc +e was respected and we played by the rules. } if ( blessed( $obj ) and $obj->isa( 'Some::Object' ) ) { # This is close to your original and is slightly deprecated but al +so respects OO inheritance. }

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊


In reply to Re: Retrieving classname from an object by diotalevi
in thread Retrieving classname from an object by monkeyhelper

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.