开发者

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:

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?


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 be MyClass.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜