简体   繁体   English

如何更新继承的驼鹿类的元信息?

[英]how to update meta information of inherited moose classes?

I dont know if i asked that question the right way but i will try to explain. 我不知道我是否以正确的方式提出这个问题,但我会尝试解释。

I have a base class MyClass.pm: 我有一个基类MyClass.pm:

use MooseX::Declare;

class MyClass {
    method test_it {
        for (__PACKAGE__->meta->get_all_methods){
            print $_->name . "\n";
        }
    }
}

And another class MyOtherClass.pm: 另一个类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: 和脚本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. 因此,如果我在MyClass上调用test_it,则输出包含预期的“test_it”以及一些内置方法。 Calling test_it on MyOtherClass produces the same output with one, two and three missing. 在MyOtherClass上调用test_it会产生相同的输出,缺少一个,两个和三个。

How can i get a list of the methods that contains all methods of the inheriting class? 如何获取包含继承类的所有方法的方法列表?

You want $self->meta->get_all_methods , not __PACKAGE__->meta->get_all_methods . 你想要$self->meta->get_all_methods ,而不是$self->meta->get_all_methods __PACKAGE__->meta->get_all_methods __PACKAGE__ is bound by Perl at compile time, so it will always be MyClass . __PACKAGE__在编译时由Perl绑定,因此它始终是MyClass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM