我所有的 Moose 类都必须包含“namespace::autoclean”吗?和“make_immutable”或者有什么方法可以默认获得这些?
根据 Moose 最佳实践文档,我的 Moose 类应该如下所示:
package Person;
use Moose;
use namespace::autoclean;
# extends, roles, attributes, etc.
# methods
__PACKAGE__->meta->make_immutable;
1;
请参阅 Moose::手册::最佳实践。
99% 的情况下这就是我想要的,那么有没有什么方法可以让我的命名空间自动清理并且我的类默认不可变,这样我就不必让代码变得混乱?
也许存在技术原因导致不可能或不应该这样做?
谢谢
According to the Moose best practices doc, my Moose classes should look like this:
package Person;
use Moose;
use namespace::autoclean;
# extends, roles, attributes, etc.
# methods
__PACKAGE__->meta->make_immutable;
1;
See Moose::Manual::BestPractices.
And 99% of the time this is what I want, so is there some way to have my namespace autocleaned and my classes made immutable by default so I don't have to have this code clutter?
Maybe there is a technical reason why it isn't possible or why it shouldn't be done?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为唯一的避免这种情况的一种方法是使用MooseX::声明
。MooseX::Declare
是一个宏< /code> 如下所示为您的示例:
它自动插入
namespace::autoclean
并使类不可变。对于扩展类,您需要执行以下操作:
对于添加角色,您需要执行以下操作:
您可以轻松地组合这些:
您还可以获得一些其他定义的关键字,例如。
方法
:如果你确实想让你的类可变,那么它:
从技术上讲,将这一切整合在一起是很困难的。
MooseX::Declare
使用Devel::Declare
构建 Perl 解释所需的语法。因此,如果样板对您来说是个问题,那么请考虑使用
MooseX::Declare
。我已经在很多项目中使用它,没有出现任何问题,并且在快速绘制基于类的应用程序时发现它非常理想。然而大多数时候我对样板很满意,所以坚持使用标准的
Moose
。I think the onlyOne way to avoid this is to useMooseX::Declare
.MooseX::Declare
is amacro
which turns below into your example:It automatically inserts
namespace::autoclean
and makes the class immutable.For extending classes you do:
And for adding roles you do:
And you can easily combine these:
You also get some other defined keywords, for eg.
method
:If you did want to make your class mutable then its:
Technically it would be difficult to pull this all together.
MooseX::Declare
makes use ofDevel::Declare
to build the necessarily syntax for the Perl to interpret.So if the boiler plate is an issue for you then consider using
MooseX::Declare
. I've used it on a lot of projects with no issues and find it ideal when quickly sketching together a class based app. However most of the time I'm happy with the boilerplate and so stick with standardMoose
.我认为 MooseX::MakeImmutable 可以给你的。
I think MooseX::MakeImmutable can do it for you.