moose 对象中构建器子例程的参数

发布于 2024-09-10 07:55:01 字数 605 浏览 10 评论 0原文

我目前正在将构建器方法委托给扩展我的基类之一的所有对象。我面临的问题是我需要所有对象读取其自身的属性或传递一个值。

#  In Role:
has 'const_string' => (
    isa     => 'Str',
    is      => 'ro',
    default => 'test',
);

has 'attr' => (
    isa     => 'Str',
    is      => 'ro',
    builder => '_builder',
);

requires '_builder';


#  In extending object  -  desired 1
sub _builder {
    my ($self) = shift;
    #  $self contains $self->const_string
 }

#  In extending object  -  desired 2
sub _builder {
    my ($arg1, $arg2) = @_;
    #  $args can be passed somehow?
 }

目前这可能吗,还是我必须采取其他方式?

I'm currently delegating the builder method to all of the objects that extend one of my base classes. The problem that I'm facing is I need all of the objects to either read an attribute of itself or be passed in a value.

#  In Role:
has 'const_string' => (
    isa     => 'Str',
    is      => 'ro',
    default => 'test',
);

has 'attr' => (
    isa     => 'Str',
    is      => 'ro',
    builder => '_builder',
);

requires '_builder';


#  In extending object  -  desired 1
sub _builder {
    my ($self) = shift;
    #  $self contains $self->const_string
 }

#  In extending object  -  desired 2
sub _builder {
    my ($arg1, $arg2) = @_;
    #  $args can be passed somehow?
 }

Is this currently possible or am I going to have to do it some other way?

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

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

发布评论

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

评论(2

纸伞微斜 2024-09-17 07:55:01

您无法将参数传递给属性构建方法。它们由 Moose 内部结构自动调用,并且只传递一个参数——对象引用本身。构建器必须能够根据它在 $self 中看到的内容或它有权访问的环境中的任何其他内容返回其值。

您想向构建者传递什么样的论点?您可以将这些值传递给对象构造函数并将它们存储在其他属性中吗?

# in object #2:
has other_attr_a => (
    is => 'ro', isa => 'Str',
);
has other_attr_b => (
    is => 'ro', isa => 'Str',
);

sub _builder
{
    my $self = shift;
    # calculates something based on other_attr_a and other_attr_b
}

# object #2 is constructed as:
my $obj = Class2->new(other_attr_a => 'value', other_attr_b => 'value');

另请注意,如果您有基于其他属性值构建的属性,则应将它们定义为惰性,否则构建器/默认值将在对象构造时立即运行,并且以未定义的顺序运行。将它们设置为惰性会延迟它们的定义,直到第一次需要它们为止。

You can't pass arguments to attribute build methods. They are called automatically by Moose internals, and passed only one argument -- the object reference itself. The builder must be able to return its value based on what it sees in $self, or anything else in the environment that it has access to.

What sort of arguments would you be wanting to pass to the builder? Can you instead pass these values to the object constructor and store them in other attributes?

# in object #2:
has other_attr_a => (
    is => 'ro', isa => 'Str',
);
has other_attr_b => (
    is => 'ro', isa => 'Str',
);

sub _builder
{
    my $self = shift;
    # calculates something based on other_attr_a and other_attr_b
}

# object #2 is constructed as:
my $obj = Class2->new(other_attr_a => 'value', other_attr_b => 'value');

Also note that if you have attributes that are built based off of other attribute values, you should define them as lazy, otherwise the builders/defaults will run immediately on object construction, and in an undefined order. Setting them lazy will delay their definition until they are first needed.

空城旧梦 2024-09-17 07:55:01

你可以这样做:

has 'attr' => (
isa     => 'Str',
is      => 'ro',
builder => '_pre_builder',
);

sub pre_builder {
  _builder(@_, 'your_arg');
}

You can do something like this:

has 'attr' => (
isa     => 'Str',
is      => 'ro',
builder => '_pre_builder',
);

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