如何遍历Perl中类的所有方法? [英] How do I loop over all the methods of a class in Perl?

查看:127
本文介绍了如何遍历Perl中类的所有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何遍历Perl中类的所有方法?在线上有关于Perl内省或反思的好参考吗?

How do you loop over all the methods of a class in Perl? Are there any good online references to Perl introspection or reflection?

推荐答案

Todd Gardner推荐使用Moose是一项不错的建议,但是他选择的示例代码并没有很大帮助.

The recommendation Todd Gardner gave to use Moose is a good one, but the example code he chose isn't very helpful.

如果您正在使用类检查非Moose,则需要执行以下操作:

If you're inspecting a non-Moose using class, you'd do something like this:

use Some::Class;
use Class::MOP;

my $meta = Class::MOP::Class->initialize('Some::Class');

for my $meth ( $meta->get_all_methods ) {
    print $meth->fully_qualified_name, "\n";
}

请参见 Class :: MOP :: Class文档以获取有关如何进行内省的更多详细信息.

See the Class::MOP::Class docs for more details on how to do introspection.

您还将注意到,我使用的是Class :: MOP而不是Moose. Class :: MOP(MOP =元对象协议)是Moose构建的基础.如果您正在使用非Moose类,则使用Moose进行内省不会为您带来任何好处.

You'll also note that I used Class::MOP instead of Moose. Class::MOP (MOP = Meta-Object Protocol) is the base on which Moose builds. If you're working with non-Moose classes, using Moose to introspect doesn't gain you anything.

如果需要,可以使用use Moose ()Moose::Meta::Class->initialize代替CMOP.

If you wanted, you could use Moose () and Moose::Meta::Class->initialize instead of CMOP.

这篇关于如何遍历Perl中类的所有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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