I'm trying to see if keyword_objects has an element {name=>'CRASH', status=>'+'}.
# $bug is a reference to the below data
{
'keyword_objects' => [
bless( { 'name' => 'CRASH', 'status' => '+'}, 'SomeModule::SomeFilename' ),
bless( { 'name' => 'CUSTOMER', 'status' => '-' }, 'SomeModule::SomeFilename' ) ],
'category' => 'error'
}
I couldn't find something like filter in another language so my alternative was using map.
my @isCrash = map { $_->name eq 'CRASH' && $_->status eq '+' } @{ $bug->{keyword_objects} };
The problem with this is that, when there is no such keyword, every time the operation is done, it seems to return an empty value. @isCrash becomes an array of multiple empty values and if(@isCrash) becomes useless. I surely can introduce a new variable which can be changed from the map operation but I feel like there should be a better way to do it. Someone please chime in and share your knowledge.
mapwithgrep. But I have serious questions here. The elements of the arrayref are objects -- why do you create them on the fly like that? What's the purpose of having objects without any methods etc? Then they can only be queried like mere hashrefs, since there's no other tools in these ad-hoc classes ... but that's just bad practice. Does this stand for some other, more complex code?$_->nameimplies that there is a methodname("getter" for that attribute) -- so if there actually exits a class built somewhere then why do you not use its constructor (new) here?grepthere instead ofmap... :)SomeModule::SomeFilename, it has submodules and all that. Thank you a lot!