如何使用 Moose::Exporter 导出函数?
我想从我在所有子类中使用的基类导出一个简单的函数,而不必一直使用 $self->myfunc() ,只需一个简单的 func() 调用。
我尝试使用 Moose::Exporter
但我不明白
as_is => [ 'sugar3', \&Some::Random::thing ],
在做什么,因为这个例子似乎不完整。 Sugar3 没有在任何地方定义,所以我不知道在哪里或如何使用它。我现在可以在子类中调用sugar3()吗? Sugar3() 是驼鹿的秘密吗?
然后就是这个东西......字面上
thing;
是导出的,但我不知道它在做什么,因为没有它的例子。这是函数调用吗?
无论如何,更重要的是如何像通常使用 Exporter 那样导出函数,但使用 Moose Exporter, 如果我的基类在其后有 3 级继承,会发生什么情况,所有子类都会子类可以访问这个导出函数吗?
I wanted to export a simple function from a base class that I use all over my sub classes without having to use $self->myfunc() all the time, just a simple func() call.
I tried doing this with the example from Moose::Exporter
but I didnt understand what
as_is => [ 'sugar3', \&Some::Random::thing ],
was doing, as the example seems incomplete. sugar3 isnt defined anywhere, so I dunno where or how to use it. Can I call sugar3() in the subclass now? Is sugar3() some secret Moose thing?
and then the was this thing... literally
thing;
that was exported, but I have no idea what thing is doing since there is no example of it. Is this a function call?
Anyway, more to the point how do you export functions like you would normally do with Exporter, but with Moose Exporter instead, and what happens if my baseclass has 3 levels of inheritance after it, will all the sub sub classes have access to this exported function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
as_is =>; [ ... ]
:sugar3
是要导出的子项目的名称。是的,如果您将其导出到子类中,您现在可以在子类中调用
sugar3
。也就是说,将(除了常量之外的任何内容)导出到子类通常很奇怪。是的,
thing;
是一个子调用。在no strict;
下,它也可以与'thing';
相同。子类无法访问子类,除非它作为方法调用(例如
$o->thing;
而不是thing;
)。不过,导出方法非常奇怪。创建 Moose::Role 来为类提供方法。as_is => [ ... ]
:sugar3
is the name of a sub to export.Yes, you can call
sugar3
in the subclass now if that's where you exported it to. That said, it's usually weird to export (anything but constants) to a subclass.Yes,
thing;
is a sub call. Underno strict;
, it could also be the same as'thing';
.The sub classes won't have access to the sub unless it's called as a method (e.g.
$o->thing;
instead ofthing;
). It's extremely weird to export methods, though. Create a Moose::Role to give methods to a class.