如何使用 Moose 和 Perl 在参数化角色的方法中访问运行时创建的方法内的消费者类的方法?
我在参数化角色中定义了一个方法,需要在运行时创建一个新类 使用 Moose::Meta::Class->create
并将确切的参数化角色应用于它。我还在
$new_class->meta->add_method( some_name => sub {
my ($self) = @_;
...
})
sub {...} 内部使用该角色创建了一个新方法,我想访问消费者类的方法并将其用于某些用途,我尝试使用 $self->get_method,它不起作用,我该怎么做?
请注意,上面子中的 $self
是 MooseX::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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要回答你的第二个问题,原因是因为 Perl 的 OO 不允许你仅向类的一个实例添加方法,因此 Moose 必须通过使用额外方法创建一个子类并将唯一对象重新赋予该子类来伪造它子类。
请注意,如果您正确地做事并使用
isa
、has
和/或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/ordoes
rather than by trying to rely on the name of the object's blessed package, this doesn't matter. The object stillisa
some_type,has
all of some_type's attributes, anddoes
all of some_type's roles even though it's now blessed into a package with an ugly auto-generated name.听起来你的根本问题几乎正是我在 这个问题:从角色定义中,您需要获取角色所在的对象或类的类(及其元类)被应用到。这在普通角色中是不可能的,但可以通过参数化角色实现。
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.
我不太确定你想在这里做什么。假设您有
$new_class
是FooBar
的元对象。因此,如果您想向FooBar
添加一个方法,您会说这基本上与
您应该使用 Moose::Util。即使您的类没有
meta
方法或者将其用于其他用途,这也会返回正确的元对象(如果有)。如前所述,我不确定这是否能回答您的问题。
I'm not quite sure what you're trying to do here. Let's assume you have
then
$new_class
is the meta object forFooBar
. So, if you want to add a method toFooBar
you would saywhich would basically be the same as
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 ameta
method or it uses it for something else.As said, I'm not sure this answers your question.