如何更新继承的驼鹿类的元信息?
我不知道我问这个问题是否正确,但我会尽力解释。
我有一个基类 MyClass.pm:
use MooseX::Declare;
class MyClass {
method test_it {
for (__PACKAGE__->meta->get_all_methods){
print $_->name . "\n";
}
}
}
和另一个类 MyOtherClass.pm:
use MooseX::Declare;
class MyOtherClass extends MyClass {
method one {
return 1;
}
method two {
return 1;
}
method three {
return 1;
}
}
和一个脚本 use_it.pl:
#!/usr/bin/perl
use strict;
use warnings;
use MyClass;
use MyOtherClass;
my $class = MyOtherClass->new;
my $otherclass = MyOtherClass->new;
print "MyClass can:\n";
$class->test_it;
print "MyOtherClass can:\n";
$otherclass->test_it;
exit 0;
输出是:
MyClass can: dump DEMOLISHALL meta does new DESTROY BUILDALL BUILDARGS test_it DOES MyOtherClass can: dump DEMOLISHALL meta does new DESTROY BUILDALL BUILDARGS test_it DOES
因此,如果我在 MyClass 上调用 test_it ,输出将包含预期的“test_it”以及一些内置方法。 在 MyOtherClass 上调用 test_it 会产生相同的输出,但缺少 1、2 和 3。
如何获取包含继承类的所有方法的方法列表?
I dont know if i asked that question the right way but i will try to explain.
I have a base class MyClass.pm:
use MooseX::Declare;
class MyClass {
method test_it {
for (__PACKAGE__->meta->get_all_methods){
print $_->name . "\n";
}
}
}
And another class MyOtherClass.pm:
use MooseX::Declare;
class MyOtherClass extends MyClass {
method one {
return 1;
}
method two {
return 1;
}
method three {
return 1;
}
}
And a script use_it.pl:
#!/usr/bin/perl
use strict;
use warnings;
use MyClass;
use MyOtherClass;
my $class = MyOtherClass->new;
my $otherclass = MyOtherClass->new;
print "MyClass can:\n";
$class->test_it;
print "MyOtherClass can:\n";
$otherclass->test_it;
exit 0;
Output is:
MyClass can: dump DEMOLISHALL meta does new DESTROY BUILDALL BUILDARGS test_it DOES MyOtherClass can: dump DEMOLISHALL meta does new DESTROY BUILDALL BUILDARGS test_it DOES
So if i call test_it on MyClass the output contains as expected "test_it" alongside with some build in methods.
Calling test_it on MyOtherClass produces the same output with one, two and three missing.
How can i get a list of the methods that contains all methods of the inheriting class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
$self->meta->get_all_methods
,而不是__PACKAGE__->meta->get_all_methods
。__PACKAGE__
在编译时由 Perl 绑定,因此它始终是MyClass
。You want
$self->meta->get_all_methods
, not__PACKAGE__->meta->get_all_methods
.__PACKAGE__
is bound by Perl at compile time, so it will always beMyClass
.