将 MooseX::Method::Signatures 导入调用者的范围
我制作了一个“捆绑”模块,它可以做很多事情:导入 Moose
、导入 true
、namespace::autoclean
,使调用者的类不可变(取自MooseX::AutoImmute
)。我无法弄清楚的一件事是如何包含 MooseX::Method::Signatures
。
这是我到目前为止所得到的:
package My::OO;
use Moose::Exporter;
use Hook::AfterRuntime;
use Moose ();
use true ();
use namespace::autoclean ();
my ($import, $unimport, $init_meta) = Moose::Exporter->build_import_methods(
also => ['Moose'],
);
sub import {
true->import();
my $caller = scalar caller;
after_runtime { $caller->meta->make_immutable };
namespace::autoclean->import(-cleanee => $caller);
goto &$import;
}
sub unimport {
goto &$unimport;
}
1;
想法是,在我的代码中,我现在可以做这样的事情:
package My::Class; {
use My::OO;
extends 'My::Parent';
method foo() { ... }
}
但现在我仍然必须包含一个额外的 use MooseX::Method::Signatures;
。如何将其包含到我的 OO 模块中?
I've made a "bundle" module which does a bunch of things: imports Moose
, imports true
, namespace::autoclean
, makes the caller's class immutable (taken from MooseX::AutoImmute
). The one thing I haven't been able to figure out is how to include MooseX::Method::Signatures
.
Here's what I've got so far:
package My::OO;
use Moose::Exporter;
use Hook::AfterRuntime;
use Moose ();
use true ();
use namespace::autoclean ();
my ($import, $unimport, $init_meta) = Moose::Exporter->build_import_methods(
also => ['Moose'],
);
sub import {
true->import();
my $caller = scalar caller;
after_runtime { $caller->meta->make_immutable };
namespace::autoclean->import(-cleanee => $caller);
goto &$import;
}
sub unimport {
goto &$unimport;
}
1;
The idea is that in my code, I can now do things like this:
package My::Class; {
use My::OO;
extends 'My::Parent';
method foo() { ... }
}
but right now I still have to include an extra use MooseX::Method::Signatures;
. How can I include that into my OO module?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,请注意
Hook::AfterRuntime
的实现非常脆弱。虽然它适用于许多简单的事情,但很容易导致难以调试的错误。我建议您自己编写->meta->make_immutable
,或者使用其他方法来绕过编写它,例如MooseX::Declare
。回答您的实际问题:
First off, please note that the implementation of
Hook::AfterRuntime
is quite fragile. While it works for many simple things, it's extremely easy to end up with very hard to debug errors. I'd recommend just writing the->meta->make_immutable
yourself, or using other approaches to get around writing it, likeMooseX::Declare
, for example.To answer your actual question: