如何在 Moose 对象中设置一堆属性构建器?

发布于 2024-10-03 09:11:07 字数 543 浏览 2 评论 0原文

我有以下五个 Moose 属性:

has ['summary_file', 'html_file', 'url1', 'url2', 'txt_file'] => (
    is       => 'rw',
    isa      => 'Str',
    required => 0,
    lazy     => 1,
    default  => sub { confess "Attribute not set"; },
    );

我想:

  1. 让每个属性都使用自己的构建器(例如 set 'summary_file' 使用 _build_summary_file 等)
  2. 设为默认值承认子状态调用了哪个(未初始化)属性(例如“属性'summary_file'未设置”)。

我可以通过编写五个单独的 has 来完成上述任务,但也许有更紧凑的方法?

I have the following five Moose attributes:

has ['summary_file', 'html_file', 'url1', 'url2', 'txt_file'] => (
    is       => 'rw',
    isa      => 'Str',
    required => 0,
    lazy     => 1,
    default  => sub { confess "Attribute not set"; },
    );

I would like to:

  1. Make each of them use its own builder (e.g. set 'summary_file' use _build_summary_file, etc.)
  2. Make the default confess sub state which (uninitialized) attribute was called (e.g. "Attribute 'summary_file' not set").

I can accomplish the above by writing five separate has's, but perhaps there's a more compact way?

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

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

发布评论

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

评论(2

猫性小仙女 2024-10-10 09:11:08

您可以执行类似以下操作(下面的“不起作用”评论后的新工作示例):

package My::Class;
use Moose;
use namespace::autoclean;

for my $attr (qw(x y)) {
    has $attr => (
        is       => 'rw',
        isa      => 'Str',
        required => 0,
        lazy     => 1,
        builder  => "_build_$attr",
    );
}

sub _build_x { rand }
sub _build_y { rand }

__PACKAGE__->meta->make_immutable;

package main;

use strict; use warnings;

my $o = My::Class->new;

print $o->$_, "\n" for qw(x y);

请注意,您不能同时指定 defaultbuilder .

@Oesor 在评论中指出了一些我忘记的事情:

has ['summary_file', 'html_file', 'url1', 'url2', 'txt_file'] => (
    is         => 'rw',
    isa        => 'Str',
    required   => 0,
    lazy_build => 1,
);

You could do something the like following (new working example after your "does not work" comment below):

package My::Class;
use Moose;
use namespace::autoclean;

for my $attr (qw(x y)) {
    has $attr => (
        is       => 'rw',
        isa      => 'Str',
        required => 0,
        lazy     => 1,
        builder  => "_build_$attr",
    );
}

sub _build_x { rand }
sub _build_y { rand }

__PACKAGE__->meta->make_immutable;

package main;

use strict; use warnings;

my $o = My::Class->new;

print $o->$_, "\n" for qw(x y);

Note that you cannot specify both a default and a builder.

@Oesor points out in a comment something I forgot:

has ['summary_file', 'html_file', 'url1', 'url2', 'txt_file'] => (
    is         => 'rw',
    isa        => 'Str',
    required   => 0,
    lazy_build => 1,
);
玻璃人 2024-10-10 09:11:08

如果您正在寻找一个在未设置值时在访问时引发异常的属性,请查看 MooseX::LazyRequire

如果您正在寻找一个构建器在调用时抛出警告,请将该语句包含在构建器方法中...或者包装访问器/读取器方法来执行此操作。 (例如“before 'attribute_name' => sub { ...complain... };”)

如果您正在寻找一种使用相同命名约定为属性指定构建器方法的方法lazy_build 确实如此,请参阅 MooseX::AttributeShortcuts (支持 'builder => 1' 为 'builder => "_build_${attribute_name}"')。

If you're looking for an attribute to throw an exception on access when it does not have a value set, look at MooseX::LazyRequire.

If you're looking for a builder to throw an warning if it's called, include that statement in the builder method... OR wrap the accessor/reader method to do that. (e.g. "before 'attribute_name' => sub { ...complain... };")

If you're looking for an way to specify builder methods to an attribute using the same naming convention lazy_build does, see MooseX::AttributeShortcuts (supporting 'builder => 1' as 'builder => "_build_${attribute_name}"').

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