如何使用 Moose 和 Perl 在参数化角色的方法中访问运行时创建的方法内的消费者类的方法?

发布于 2024-08-24 04:03:33 字数 711 浏览 12 评论 0原文

我在参数化角色中定义了一个方法,需要在运行时创建一个新类 使用 Moose::Meta::Class->create 并将确切的参数化角色应用于它。我还在

$new_class->meta->add_method( some_name => sub {
 my ($self) = @_;
 ...
})

sub {...} 内部使用该角色创建了一个新方法,我想访问消费者类的方法并将其用于某些用途,我尝试使用 $self->get_method,它不起作用,我该怎么做?

请注意,上面子中的 $selfMooseX::Role::Parameterized::Meta::Role::Parameterized

我还有另一个问题,如果我这样做的话this:

my $object = Moose::Meta::Class->create(
       "some_type",

);

为什么 $object 不是 some_type 类型,它是一些丑陋的 MooseX::Role::Parameterized::Meta::Role::Parameterized 以及我如何得到到 some_type 类型的对象?

I define a method inside a parametrized role that needs to create a new class at run time
using Moose::Meta::Class->create and apply that exact parametrized role to it. I am also making a new method for that role using

$new_class->meta->add_method( some_name => sub {
 my ($self) = @_;
 ...
})

inside the sub {...} I want to access a method of the consumer class and use it for something, I have tried using $self->get_method, it didn't work, how do I do this?

Please notice that the $self inside the sub above is MooseX::Role::Parameterized::Meta::Role::Parameterizable

I also have another question, if I do this:

my $object = Moose::Meta::Class->create(
       "some_type",

);

Why isn't $object of type some_type and it's some ugly MooseX::Role::Parameterized::Meta::Role::Parameterizable and how do I get to the object of type some_type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

嘿看小鸭子会跑 2024-08-31 04:03:33

要回答你的第二个问题,原因是因为 Perl 的 OO 不允许你仅向类的一个实例添加方法,因此 Moose 必须通过使用额外方法创建一个子类并将唯一对象重新赋予该子类来伪造它子类。

请注意,如果您正确地做事并使用 isahas 和/或 does 进行内省,而不是尝试依赖对象的祝福包的名称,这并不重要。该对象仍然isa some_type,具有 some_type 的所有属性,并且执行 some_type 的所有角色,即使它现在被祝福到一个丑陋的包中自动生成的名称。

To answer your second question, the reason is because Perl's OO doesn't allow you to add a method to just one instance of a class, so Moose has to fake it by creating a subclass with the extra method and reblessing the unique object into that subclass.

Note that, if you are doing things correctly and doing your introspection with isa, has, and/or does rather than by trying to rely on the name of the object's blessed package, this doesn't matter. The object still isa some_type, has all of some_type's attributes, and does all of some_type's roles even though it's now blessed into a package with an ugly auto-generated name.

尘曦 2024-08-31 04:03:33

听起来你的根本问题几乎正是我在 这个问题:从角色定义中,您需要获取角色所在的对象或类的类(及其元类)被应用到。这在普通角色中是不可能的,但可以通过参数化角色实现。

It sounds like your underlying problem is nearly exactly what I described at this question: from within the role definition, you need to get at the class (and its meta-class) of the object or class the role is being applied to. This isn't possible from within normal roles, but it's possible through parameterized roles.

守不住的情 2024-08-31 04:03:33

我不太确定你想在这里做什么。假设您有

my $new_class = Moose::Meta::Class->create('FooBar');

$new_class FooBar 的元对象。因此,如果您想向 FooBar 添加一个方法,您会说

$new_class->add_method(foo => sub { … });

这基本上与

FooBar->meta->add_method(foo => sub { … });

您应该使用 Moose::Util。即使您的类没有 meta 方法或者将其用于其他用途,这也会返回正确的元对象(如果有)。

如前所述,我不确定这是否能回答您的问题。

I'm not quite sure what you're trying to do here. Let's assume you have

my $new_class = Moose::Meta::Class->create('FooBar');

then $new_class is the meta object for FooBar. So, if you want to add a method to FooBar you would say

$new_class->add_method(foo => sub { … });

which would basically be the same as

FooBar->meta->add_method(foo => sub { … });

You should also probably use find_meta() from Moose::Util. This will return the correct meta object (if there is one) even if your class doesn't have a meta method or it uses it for something else.

As said, I'm not sure this answers your question.

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