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;
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?