Moose:扩展导出器会导致构造函数消失?

发布于 2024-08-12 19:41:32 字数 540 浏览 5 评论 0原文

这是我无法弄清楚的奇怪事情。我有一个 Moose 类,我还想使用古老的标准 Exporter 模块导出一些常量。但是,一旦我将 extends Exporter 添加到我的类中,出于某种原因,默认的 Moose 构造函数就不再被继承,并且我收到致命错误:

Can't locate object method "new" via package "MyApp::Dispatch"

这是一个演示该问题的简单测试。

package Foo;
use Moose;
use constant NARF => 'poit';
extends 'Exporter';

1;

$ perl -I./lib -MFoo -e '$f=Foo->new'
Can't locate object method "new" via package "Foo" at -e line 1.

删除 extends 行可以使问题消失。

这是使用 Moose 0.93。

Here's something weird that I can't figure out. I have a Moose class that I also want to export some constants, using the age-old standard Exporter module. However, as soon as I add extends Exporter to my class, for some reason, the default Moose constructor no longer gets inherited and I get the fatal error:

Can't locate object method "new" via package "MyApp::Dispatch"

Here is a simple test which demonstrates the problem.

package Foo;
use Moose;
use constant NARF => 'poit';
extends 'Exporter';

1;

$ perl -I./lib -MFoo -e '$f=Foo->new'
Can't locate object method "new" via package "Foo" at -e line 1.

Removing the extends line makes the problem vanish.

This is using Moose 0.93.

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

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

发布评论

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

评论(2

时光沙漏 2024-08-19 19:41:32

它假设您从另一个基于 Moose 的类继承,因此它不会从 Moose::Object 继承。我不确定标准答案是什么:只需手动添加 Moose::Object 或以某种方式使用 MooseX::NonMoose 或其他东西。

但即使没有继承,Exporter 也能正常工作;只需导入其导入例程:

use Exporter "import";

It is assuming you are inheriting from another Moose-based class, so it doesn't inherit from Moose::Object. I'm not sure what the standard answer would be: just manually adding Moose::Object or somehow using MooseX::NonMoose or something else.

But Exporter works just fine even when not inherited; just import its import routine:

use Exporter "import";
四叶草在未来唯美盛开 2024-08-19 19:41:32

您已经得到了真正的答案,但真正的问题是为什么您有一个导出内容并且是对象的模块?这是一个非常奇怪的设计,并且可能是一种代码味道。

You already got the real answer, but the real question is why do you have a module that exports stuff and is an object? That's a pretty weird design, and may be a code smell.

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