Perl: oo with use parent - checking if class has a parent -
i have perl objects built time ago not moose, bless, inheritance implemented using 'parent' pragma.
now know whether there way check whether class has used 'parent' or not.
e.g. if have 2 classes
package animal; sub new { $class = shift; return bless {}, $class; } 1;
and
package cat; use parent 'animal'; sub new { $class = shift; return bless {}, $class; } 1;
would there check make determine 'cat' class has parent ( not care which, not ), , animal not, given $foo either of them?
i can't picture why you'd ever want know this, it's possible using following:
use mro; $inherits = @{ mro::get_linear_isa($class) } > 1;
or
my $isa = { no strict 'refs'; \@{ $class . '::isa' } }; $inherits = @$isa;
notes:
- all classes inherit universal, that's ignored unless class explicitly declares inherits it.
- these methods don't care how inheritance declared (
use parent
or other means).
Comments
Post a Comment