有没有什么办法可以在Moose对象中使用Moose :: Exporter?(Is there any way I can use Moose::Exporter from within a Moos

编程入门 行业动态 更新时间:2024-10-11 13:19:20
有没有什么办法可以在Moose对象中使用Moose :: Exporter?(Is there any way I can use Moose::Exporter from within a Moose object?)

我正在寻找一种方法来设置从一个父Moose类,而不是一个独立的实用程序类中的一些辅助方法。 如果可能的话,这将是一个更加透明的方式来为模块添加驼鹿糖,因为它不需要明确地需要任何帮助模块(因为所有的东西都会通过extends声明来)。

根据文档中提供的示例 ,这大致是我要做的:

package Parent; use Moose; Moose::Exporter->setup_import_methods( with_meta => [ 'has_rw' ], as_is => [ 'thing' ], also => 'Moose', ); sub has_rw { my ( $meta, $name, %options ) = @_; $meta->add_attribute( $name, is => 'rw', %options, ); } # then later ... package Child; use Moose; extends 'Parent'; has 'name'; has_rw 'size'; thing;

但是这不起作用:

perl -I. -MChild -wle'$obj = Child->new(size => 1); print $obj->size' String found where operator expected at Child.pm line 10, near "has_rw 'size'" (Do you need to predeclare has_rw?) syntax error at Child.pm line 10, near "has_rw 'size'" Bareword "thing" not allowed while "strict subs" in use at Child.pm line 12. Compilation failed in require. BEGIN failed--compilation aborted.

PS。 我也尝试将导出魔法移动到角色中( with Role;而不是extends Parent; )但是会出现相同的错误。

I'm looking for a way to set up some helper methods from within a parent Moose class, rather than a standalone utility class. If it is possible, it would be a more transparent way of adding Moose sugar to modules, as it does not require explicitly requiring any helper modules (as everything would come via the extends declaration).

Based on the example provided in the documentation, this is roughly what I am going for:

package Parent; use Moose; Moose::Exporter->setup_import_methods( with_meta => [ 'has_rw' ], as_is => [ 'thing' ], also => 'Moose', ); sub has_rw { my ( $meta, $name, %options ) = @_; $meta->add_attribute( $name, is => 'rw', %options, ); } # then later ... package Child; use Moose; extends 'Parent'; has 'name'; has_rw 'size'; thing;

However this does not work:

perl -I. -MChild -wle'$obj = Child->new(size => 1); print $obj->size' String found where operator expected at Child.pm line 10, near "has_rw 'size'" (Do you need to predeclare has_rw?) syntax error at Child.pm line 10, near "has_rw 'size'" Bareword "thing" not allowed while "strict subs" in use at Child.pm line 12. Compilation failed in require. BEGIN failed--compilation aborted.

PS. I've also tried moving the export magic into a role (with Role; rather than extends Parent;) but the same errors occur.

最满意答案

这是不受支持的,并且有充分的理由。 一个类或一个角色与糖方法不同,在某种程度上,不同的事物应该是不同的。 如果你的问题需要“使用”Moose +一个定制Sugar包,那么你可以通过简单地将你的定制糖包出口到Moose来解决这个问题,从你的例子中窃取:

package MySugar; use strict; use Moose::Exporter; Moose::Exporter->setup_import_methods( with_meta => [ 'has_rw' ], as_is => [ 'thing' ], also => 'Moose', ); sub has_rw { my ( $meta, $name, %options ) = @_; $meta->add_attribute( $name, is => 'rw', %options, ); }

然后你简单地说:

package MyApp; use MySugar; # imports everything from Moose + has_rw and thing extends(Parent); has_rw 'name'; has 'size'; thing;

这就是MooseX::POE工作方式,以及其他几个软件包。 对于一个人来说,我会反对extends带糖,就像你在这里暗示的那样,因为一个阶级不是一束糖的功能,而且这两个人真的不应该混淆。

更新:同时引入最简洁的方法是将Parent重做为应用于Moose :: Object的角色。

package Parent::Methods; use 5.10.0; use Moose::Role; sub something_special { say 'sparkles' }

然后我们简单地将调用更改为MySugar中的Moose :: Exporter

Moose::Exporter->setup_import_methods( apply_base_class_roles => 'Parent::Methods', with_meta => ['has_rw'], as_is => ['thing'], also => 'Moose', );

现在你可以简单地说

package MyApp; use MySugar; has_rw 'name'; has 'size'; thing; package main; MyApp->new->something_special; # prints sparkles

我相信你想要的最终细节。

This isn't supported, and there is a good reason why. A Class or a Role are not the same as sugar methods, and at some level different things should be different. If your problem is having to "use" Moose + A Custom Sugar package then you can solve that by simply having your custom sugar package export Moose as well, stealing from your example:

package MySugar; use strict; use Moose::Exporter; Moose::Exporter->setup_import_methods( with_meta => [ 'has_rw' ], as_is => [ 'thing' ], also => 'Moose', ); sub has_rw { my ( $meta, $name, %options ) = @_; $meta->add_attribute( $name, is => 'rw', %options, ); }

Then you simply say:

package MyApp; use MySugar; # imports everything from Moose + has_rw and thing extends(Parent); has_rw 'name'; has 'size'; thing;

This is how MooseX::POE works, as well as several other packages. I for one would argue against having extends bring in the sugar like you're suggesting here because a Class is not a bundle of sugar functions, and the two really should never be confused.

UPDATE: To bring in both at once the cleanest approach is to rework Parent as a role that is applied to Moose::Object.

package Parent::Methods; use 5.10.0; use Moose::Role; sub something_special { say 'sparkles' }

Then we simply change the call to Moose::Exporter in MySugar to look like

Moose::Exporter->setup_import_methods( apply_base_class_roles => 'Parent::Methods', with_meta => ['has_rw'], as_is => ['thing'], also => 'Moose', );

Now you can simply say

package MyApp; use MySugar; has_rw 'name'; has 'size'; thing; package main; MyApp->new->something_special; # prints sparkles

Which is I believe the final detail you were wanting.

更多推荐

本文发布于:2023-08-03 18:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有没有什么   象中   办法   Moose   Exporter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!