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

发布于 2024-10-11 15:30:13 字数 1149 浏览 6 评论 0原文

我不知道我问这个问题是否正确,但我会尽力解释。

我有一个基类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-10-18 15:30:13

您需要 $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 be MyClass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文