What is the best practice for defining all superclasses of the Perl class?

Is there a standard CPAN way to define all superclasses of the Perl class (or, best of all, the whole superclass tree, before UNIVERSAL)?

Or is it best to just learn @{"${$class}::ISA"} for each class, class parents, etc ....?

+7
source share
4 answers

There is no standard way because this is not the standard thing you want to do. For anything but visualization, this is the red OO flag that wants to test your inheritance tree.

In addition to Class :: ISA, there is mro :: get_linear_isa () . Both were in the kernel for a while, so they could be considered "standard" for some definition. Both of them show inheritance as a flat list, not a tree, which is mainly useful for deep magic.

The perl5i meta object provides both linear_isa () , like mro (it just calls mro), and ISA () , which returns the class' @ISA . It can be used to build a tree using simple recursion without going to symbol tables.

 use perl5i::2; func print_isa_tree($class, $depth) { $depth ||= 0; my $indent = " " x $depth; say $indent, $class; for my $super_class ($class->mc->ISA) { print_isa_tree($super_class, $depth+1); } return; } my $Class = shift; $Class->require; print_isa_tree($Class); __END__ DBIx::Class DBIx::Class::Componentised Class::C3::Componentised DBIx::Class::AccessorGroup Class::Accessor::Grouped 
+8
source

I think Class :: ISA is what you are looking for

 use Class::ISA; use Mojolicious; print join "\n", Class::ISA::super_path("Mojolicious"); 

Print

 Mojo Mojo::Base 

However, this is not some kind of “best practice,” since the whole task is not what Perl programmers do every day.

+4
source

I do not believe that there is something like a “standard CPAN method”. Considering @ISA is common practice - and also believable, as methods like use base qw(...) and use parent -norequire, ... also work on top of @ISA ...

+1
source

Most likely, these days you want to use one of the functions from mro , for example mro::get_linear_isa .

 use mro; my @superclasses = mro::get_linear_isa($class); 
0
source

All Articles