如何同时分配多个 Moose 属性?

发布于 2024-08-17 08:41:58 字数 515 浏览 7 评论 0原文

我逐渐对一些代码进行了 Moose 化,这些代码从分隔的管道中读取行,分割每个行并使用哈希切片分配将它们添加到哈希中。

我已将哈希转换为 Moose 类,但现在我不知道如何快速将文件中的字段分配给该类的属性(如果有的话)。

我知道我可以很容易地做到:

my $line = get_line_from_file;
my @fields = split /\|/, $line;
my $record = My::Record->new;
$record->attr1($fields[0]);
...

但我希望有一个快速的衬里一次性分配所有属性,有点类似于:

my $line = get_line_from_file;
my %records;
@records{@field_names} = split /\|/, $line;

我读过有关强制的内容,但据我所知,这不是我想要的。

是否可以?

谢谢

I'm gradually Moose-ifying some code that reads lines from a pipe delimited, splits each and assigns adds them to a hash using a hash slice.

I've turned the hash into a Moose class but now I have no idea how to quickly assign the fields from the file to the attributes of the class (if at all).

I know I can quite easily just do:

my $line = get_line_from_file;
my @fields = split /\|/, $line;
my $record = My::Record->new;
$record->attr1($fields[0]);
...

but I was hoping for a quick one liner to assign all the attributes in one go, somewhat akin to:

my $line = get_line_from_file;
my %records;
@records{@field_names} = split /\|/, $line;

I've read about coercion but from what I can tell it's not what I'm after.

Is it possible?

Thanks

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

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

发布评论

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

评论(3

咋地 2024-08-24 08:41:58

使用 List::MoreUtils 模块中的 zip 将属性传递给构造函数:

use List::MoreUtils qw/ zip /;

my $object = My::Record->new(
  zip @field_names,
      @{[ split /\|/, get_line_from_file ]}
);

Pass the attributes to the constructor using zip from the List::MoreUtils module:

use List::MoreUtils qw/ zip /;

my $object = My::Record->new(
  zip @field_names,
      @{[ split /\|/, get_line_from_file ]}
);
简美 2024-08-24 08:41:58

我认为您使用哈希切片方法走在正确的轨道上。我会做类似的事情:

my %fields;
@fields{@field_names} = split m{\|}, $line;
my $record = My::Record->new( %fields );

你也许能够想出一个粗糙的地图解决方案来实现同样的目标,但我会在可读性方面犯错误。

I think you're on the right track with the hash slice approach. I'd do something like:

my %fields;
@fields{@field_names} = split m{\|}, $line;
my $record = My::Record->new( %fields );

You might be able to come up with a gnarly map solution to achieve the same thing, but I'd err on the side of readability here.

时光暖心i 2024-08-24 08:41:58

如果对象尚未构造,您可以简单地将所有键和值传递到构造函数中:

my $line = get_line_from_file;
my %records;
@records{@field_names} = split /\|/, $line;
my $object = My::Record->new(%records);

或者如果对象已经创建并且您想要添加一些新字段:

my $line = get_line_from_file;
my %records;
@records{@field_names} = split /\|/, $line;
while (my ($key, $value) = each(%records)
{
    $object->$key($value);

    # or if you use different names for the setters than the "default":
    $object->set_value($key, $value);
}

If the object is not constructed yet, you can simply pass all the keys and values into the constructor:

my $line = get_line_from_file;
my %records;
@records{@field_names} = split /\|/, $line;
my $object = My::Record->new(%records);

or if the object is already created and you want to add some new fields:

my $line = get_line_from_file;
my %records;
@records{@field_names} = split /\|/, $line;
while (my ($key, $value) = each(%records)
{
    $object->$key($value);

    # or if you use different names for the setters than the "default":
    $object->set_value($key, $value);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文