我所有的 Moose 类都必须包含“namespace::autoclean”吗?和“make_immutable”或者有什么方法可以默认获得这些?

发布于 2024-09-29 06:48:37 字数 486 浏览 1 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜你已久 2024-10-06 06:48:37

我认为唯一的 避免这种情况的一种方法是使用 MooseX::声明

MooseX::Declare 是一个宏< /code> 如下所示为您的示例:

use MooseX::Declare;

class Person {

    # attributes

    # methods
}

它自动插入 namespace::autoclean 并使类不可变。

对于扩展类,您需要执行以下操作:

class Person extends Human { ... }

对于添加角色,您需要执行以下操作:

class Person with BlueEyeRole { ... }

您可以轻松地组合这些:

class Person extends Human with BlueEyeRole { ... }

您还可以获得一些其他定义的关键字,例如。 方法

class Person {
    has 'name' => (is => 'rw', isa => 'Str');

    method hello { "Hello " . $self->name }
}

如果你确实想让你的类可变,那么它:

class Person is mutable { ... }

也许有技术原因导致它不可能或不应该这样做?

从技术上讲,将这一切整合在一起是很困难的。 MooseX::Declare 使用 Devel::Declare 构建 Perl 解释所需的语法。

因此,如果样板对您来说是个问题,那么请考虑使用 MooseX::Declare。我已经在很多项目中使用它,没有出现任何问题,并且在快速绘制基于类的应用程序时发现它非常理想。然而大多数时候我对样板很满意,所以坚持使用标准的 Moose

I think the only One way to avoid this is to use MooseX::Declare.

MooseX::Declare is a macro which turns below into your example:

use MooseX::Declare;

class Person {

    # attributes

    # methods
}

It automatically inserts namespace::autoclean and makes the class immutable.

For extending classes you do:

class Person extends Human { ... }

And for adding roles you do:

class Person with BlueEyeRole { ... }

And you can easily combine these:

class Person extends Human with BlueEyeRole { ... }

You also get some other defined keywords, for eg. method:

class Person {
    has 'name' => (is => 'rw', isa => 'Str');

    method hello { "Hello " . $self->name }
}

If you did want to make your class mutable then its:

class Person is mutable { ... }

Maybe there is a technical reason why it isn't possible or why it shouldn't be done?

Technically it would be difficult to pull this all together. MooseX::Declare makes use of Devel::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 standard Moose.

时光病人 2024-10-06 06:48:37

我认为 MooseX::MakeImmutable 可以给你的。

I think MooseX::MakeImmutable can do it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文