开发者

Moose: Extending Exporter causes constructor to disappear?

开发者 https://www.devze.com 2022-12-12 20:49 出处:网络
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

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 '$开发者_C百科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.


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";


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消