Perl - is object instance of class? -
is there way check whether object instance of class? object instance of class if has been blessed class:
package example; sub new { $self = {}; bless($self, shift); return $self; } ############ use example; $exp = example->new(); # $exp 'instance' of example # instanceof($exp, example) return 1
the blessed() subroutine scalar::util returns name of class object belongs to.
say blessed $exp; # prints "example" for test, use like:
if (blessed $exp eq 'example') { ...} if subclassing issue, use isa() instead.
if ($exp->isa('example')} { ... } but throws error if $exp isn't object, protect call calling blessed() first.
if (blessed($exp) , $exp->isa('example')) { ... } isa() method on universal class , every class subclass of universal, every object has isa() method.
Comments
Post a Comment