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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |