驼鹿:对象数组->通过属性循环 [英] Moose: Array of Objects->loop through Attribute

查看:99
本文介绍了驼鹿:对象数组->通过属性循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Perl Moose的新手,我正在尝试实现这一简单任务.我定义了我的Moose类"TestObject":

I'm new to Perl Moose, and I'm trying to achieve this simple task. I have my Moose class "TestObject" defined:

package TestObject;
use Moose;
use namespace::autoclean;

has 'Identifier' => (is =>'ro',isa=>'Str');

around BUILDARGS => sub
{
    my $orig = shift;
    my $class = shift;

    if ( @_ == 1 && ! ref $_[0] ) {
        return $class->$orig(Identifier => $_[0]);
    }
    else {
        return $class->$orig(@_);
    }
};
__PACKAGE__->meta->make_immutable;
1;

在另一个脚本中,我试图直接从"TestObjects"数组访问属性"Identifier":

In another script I'm trying to access the attribute "Identifier" directly from an array of "TestObjects":

use TestObject;
use experimental 'smartmatch';
my @aArray1=(TestObject->new("z003"),TestObject->new("t302"),TestObject->new("r002"));
my $sIdent="t302";
if($sIdent~~@aArray1->Identifier)
{
    print "Element with Identifier".$sIdent." found.";
}

这不起作用.我可以实现这样的解决方法:

This doesn't work. I could implement a workaround like this:

my @aIdent=();
foreach my $sObject(@aArray1)
{
    push(@aIdent,$sObject->Identifier);
}
if($sIdent~~@aIdent)
{
    print "Element with Identifier".$sIdent." found.";
}

但这似乎不是最优雅的解决方案.解决此问题的最优雅的解决方案是什么?

but that doesn't seem to be the most elegant solution. What is the most elegant solution to solve this problem?

推荐答案

请勿使用smartmatch运算符执行此操作.这是出于实验目的,可能会从将来的Perl版本中删除,或者像以前一样更改其工作方式.

Do not do this with the smartmatch operator. It's experimental for a reason, and it might be removed from future Perl versions, or change the way it works, as it's done before.

相反,这可以通过简单的 grep 来实现.

Instead, this can be achieved with a simple grep.

my @aArray1 = ( 
    TestObject->new("z003"), 
    TestObject->new("t302"),
    TestObject->new("r002"),
);

my $sIdent = "t302";
if ( grep { $_->Identifier eq $sIdent } @aArray1 ) {
    print "Element with Identifier" . $sIdent . " found.";
}

如果您希望将其缩短一点,还可以使用 first列表::实用程序.这样会更快一些,因为它将在第一局比赛结束后停止寻找.

If you want that to be a bit shorter, you can also use first from List::Util. This is a bit faster as it will stop looking after the first match.

use List::Util 'first';
my @aArray1 = ( 
    TestObject->new("z003"), 
    TestObject->new("t302"),
    TestObject->new("r002"),
);

my $sIdent = "t302";
if ( first { $_->Identifier eq $sIdent } @aArray1 ) {
    print "Element with Identifier" . $sIdent . " found.";
}


关于您的代码的几点建议:


A few words of advice on your code:

  • 永远不要使用 object 为类命名.这将使您,将来的您和维护人员感到困惑.如果您不了解 class object 之间的区别,请仔细阅读.
  • 按照惯例,Perl中的变量名和函数始终以小写形式编写,我们使用蛇形大写形式.驼峰式保护套保留用于包装名称.
  • Do not ever name a class anything with object. It is going to confuse you, future you and the maintenance guy. If you do not understand the difference between class and object, read up on that please.
  • Variable names and functions in Perl are always written in lower case by convention, and we use snake case. Camel case is reserved for package names.

这篇关于驼鹿:对象数组->通过属性循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆