找出Perl类的所有超类的最佳实践是什么? [英] What is the best practice for finding out all superclasses of a Perl class?

查看:107
本文介绍了找出Perl类的所有超类的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一种标准的CPAN方法来找出Perl类的所有超类(或者更好的是整个超类树,直到UNIVERSAL)?

Is there a standard CPAN way of finding out all the superclasses of a Perl class (or better yet entire superclass tree, up to UNIVERSAL)?

还是对每个班级,班级的父母等简单地检查@{"${$class}::ISA"}的最佳实践??

Or is the best practice to simply examine @{"${$class}::ISA"} for each class, class's parents etc...?

推荐答案

没有标准方法",因为这不是您要执行的标准操作.除了可视化之外,要检查继承树是一个OO危险信号.

There is no "standard way" because this is not a standard thing you want to do. For anything other than visualization it is an OO red flag to want to inspect your inheritance tree.

除了Class :: ISA,还有

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

perl5i元对象提供了两个

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 construct a tree using simple recursion without getting into 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

这篇关于找出Perl类的所有超类的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆