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

查看:52
本文介绍了查找 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?

推荐答案

没有标准方式",因为这不是您想要做的标准事情.对于可视化以外的任何事情,想要检查您的继承树都是面向对象的危险信号.

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,还有mro::get_linear_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 元对象提供了linear_isa(),就像 mro(它只是调用 mro)和 ISA() 返回类'@ISA.它可用于使用简单的递归构造一棵树,而无需进入符号表.

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天全站免登陆