我如何动态获取(非Moose)Perl库中的方法列表?
我正在尝试使用委托将Moose类包装在Moose类中。目前我正在编写我委托的方法列表。这不是什么大问题,但我想知道是否有更优雅的方式。
答案 0 :(得分:5)
不可能。没有办法区分sub是否是一种方法。如果你可以改为列出潜艇,你可以使用
grep exists(&{ 'Foo::Bar::'.$_ }), keys %Foo::Bar::
当然,这不会扫描继承树。如果您想这样做,您可以使用
获取要扫描的软件包列表mro::get_linear_isa('Foo::Bar')
答案 1 :(得分:4)
嗯,严格来说,无法区分潜艇和方法,正如 ikegami 所提到的那样。
但是如果你有创意,你总是可以使用PadWalker's peek_sub
和closed_over
来排序在绝大多数情况下区分子方法,
虽然当然都有可能出现误报和漏报:
use strict;
use warnings;
use PadWalker;
my $self;
sub test_sub
{
my $x = shift;
print $self;
}
sub test_method
{
my $self = shift;
}
sub is_method
{
my $coderef = shift;
my $all = PadWalker::peek_sub($coderef);
my $closed = PadWalker::closed_over($coderef);
for my $k (qw($self $me $class)) {
return 1 if $all->{$k} && !$closed->{$k};
}
return 0;
}
print "test_sub: ", is_method(\&test_sub) ?
"is a method\n" : "is a sub\n";
print "test_method: ", is_method(\&test_method) ?
"is a method\n" : "is a sub\n";
获取coderefs留给读者练习。
答案 2 :(得分:4)
ikegami回答的另一种方法是使用Package::Stash
模块。它的list_all_symbols
方法将为您提供给定包中的所有子名称。这是一个例子:
perl -MPackage::Stash -E "say for Package::Stash->new('Package::Stash')->list_all_symbols('CODE')"
答案 3 :(得分:1)
如果您已经使用过Moose,则可以使用Class :: MOP。我认为Class :: MOP是最干净的方法,您也可以从继承中获取所有方法/函数。但就像其他人所说的那样,你无法区分方法和功能。
use Class::MOP;
use Test::More;
my $class = Class::MOP::Class->initialize('Test::More');
my @methods = $class->get_method_list(); # Only Methods in this Class
my @all_methods = $class->get_all_methods(); # Include all inherited methods
printf "%d functions in Test::More\n", scalar @methods;
printf "%d functions including inheritance\n", scalar @all_methods;
print "methods: @methods\n\n";
print "all methods: ", join(' ', map({ $_->fully_qualified_name } @all_methods)), "\n";
这些打印出这样的想法:
39 functions in Test::More
49 functions including inheritance
methods: subtest _whoa cmp_ok isa_ok use_ok isnt _format_stack like _eq_array plan _eval fail _carp done_testing _type diag _is_module_name import_extra eq_array require_ok BAIL_OUT _dne is ok _eq_hash explain todo todo_skip unlike new_ok eq_set pass can_ok eq_hash is_deeply note _equal_nonrefs _deep_check skip
all methods: Exporter::export_fail Test::More::_whoa Test::More::subtest Exporter::export_tags Test::More::cmp_ok Test::More::use_ok Test::More::isa_ok Test::More::_format_stack Test::More::isnt Test::More::like Test::More::_eq_array Test::More::plan Exporter::export Test::More::_eval Test::More::_carp Test::More::fail Test::More::done_testing Test::More::_type Test::More::diag Test::Builder::Module::_strip_imports Test::More::_is_module_name Test::More::import_extra Exporter::as_heavy Test::More::eq_array Test::More::require_ok Exporter::export_to_level Test::More::BAIL_OUT Test::More::_dne Test::More::is Test::More::ok Exporter::export_ok_tags Test::More::explain Test::More::_eq_hash Test::More::todo_skip Test::More::todo Test::More::new_ok Test::More::unlike Test::More::pass Test::More::eq_set Test::More::can_ok Test::More::eq_hash Test::More::is_deeply Test::More::note Test::Builder::Module::import Test::More::_equal_nonrefs Test::More::skip Test::More::_deep_check Exporter::require_version Test::Builder::Module::builder