如何使用 Moose::Exporter 导出函数?

发布于 2024-12-09 22:47:06 字数 640 浏览 1 评论 0原文

我想从我在所有子类中使用的基类导出一个简单的函数,而不必一直使用 $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 技术交流群。

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

发布评论

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

评论(1

忱杏 2024-12-16 22:47:06

as_is =>; [ ... ]

此函数名称或子引用列表将按原样导出。您可以通过引用来识别子例程,这可以方便地通过引用直接重新导出其他模块的函数(\&Some::Package::function)。

sugar3 是要导出的子项目的名称。

是的,如果您将其导出到子类中,您现在可以在子类中调用 sugar3 。也就是说,将(除了常量之外的任何内容)导出到子类通常很奇怪。

是的,thing; 是一个子调用。在 no strict; 下,它也可以与 'thing'; 相同。

子类无法访问子类,除非它作为方法调用(例如 $o->thing; 而不是 thing;)。不过,导出方法非常奇怪。创建 Moose::Role 来为类提供方法。

as_is => [ ... ]:

This list of function names or sub references will be exported as-is. You can identify a subroutine by reference, which is handy to re-export some other module's functions directly by reference (\&Some::Package::function).

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. Under no 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 of thing;). It's extremely weird to export methods, though. Create a Moose::Role to give methods to a class.

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