Moose:扩展导出器会导致构造函数消失?
这是我无法弄清楚的奇怪事情。我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它假设您从另一个基于 Moose 的类继承,因此它不会从 Moose::Object 继承。我不确定标准答案是什么:只需手动添加 Moose::Object 或以某种方式使用 MooseX::NonMoose 或其他东西。
但即使没有继承,Exporter 也能正常工作;只需导入其导入例程:
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:
您已经得到了真正的答案,但真正的问题是为什么您有一个导出内容并且是对象的模块?这是一个非常奇怪的设计,并且可能是一种代码味道。
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.