如何在 Moose 中定义默认属性属性值?

发布于 2024-10-07 14:28:12 字数 431 浏览 3 评论 0原文

正如标题所示,我希望能够在我的类中执行类似的操作:

use MooseX::Declare;

class MyClass {
    default_attribute_propeties(
        is       => 'ro',
        lazy     => 1,
        required => 1,
    );

    has [qw( some standard props )] => ();

    has 'override_default_props' => (
        is       => 'rw',
        required => 0,
        ...
    );

    ...
}

即定义一些默认属性值,这些值将应用于所有属性定义,除非被覆盖。

As the title suggests, I'd like to be able to do something like this in my class:

use MooseX::Declare;

class MyClass {
    default_attribute_propeties(
        is       => 'ro',
        lazy     => 1,
        required => 1,
    );

    has [qw( some standard props )] => ();

    has 'override_default_props' => (
        is       => 'rw',
        required => 0,
        ...
    );

    ...
}

That is, define some default property values that will apply to all attribute definitions unless overridden.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

荒岛晴空 2024-10-14 14:28:12

听起来您想编写一些自定义属性声明,提供一些默认选项。 Moose::Cookbook::Extending:: 对此进行了介绍Recipe1,例如:

package MyApp::Mooseish;

use Moose ();
use Moose::Exporter;

Moose::Exporter->setup_import_methods(
    install     => [ qw(import unimport init_meta) ],
    with_meta   => ['has_table'],
    also        => 'Moose',
);

sub has_table
{
    my ($meta, $name, %config) = @_;

    $meta->add_attribute(
        $name,

        # overridable defaults.
        is => 'rw',
        isa => 'Value', # any defined non-reference; hopefully the caller
                        # passed their own type, which will override
                        # this one.
        # other options you may wish to supply, or calculate based on
        # other arguments passed to this function...

        %config,
    );
}

然后在你的班级中:

package MyApp::SomeObject;

use MyApp::Moosish;

has_table => (
    # any normal 'has' options;
    # will override the defaults.
);

# remaining class definition as normal.

It sounds like you want to write some custom attribute declarations, that provide some default options. This is covered in Moose::Cookbook::Extending::Recipe1, e.g.:

package MyApp::Mooseish;

use Moose ();
use Moose::Exporter;

Moose::Exporter->setup_import_methods(
    install     => [ qw(import unimport init_meta) ],
    with_meta   => ['has_table'],
    also        => 'Moose',
);

sub has_table
{
    my ($meta, $name, %config) = @_;

    $meta->add_attribute(
        $name,

        # overridable defaults.
        is => 'rw',
        isa => 'Value', # any defined non-reference; hopefully the caller
                        # passed their own type, which will override
                        # this one.
        # other options you may wish to supply, or calculate based on
        # other arguments passed to this function...

        %config,
    );
}

And then in your class:

package MyApp::SomeObject;

use MyApp::Moosish;

has_table => (
    # any normal 'has' options;
    # will override the defaults.
);

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