Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: search for similiar text in Moose Attributesby stephen (Priest) |
on May 31, 2013 at 19:22 UTC ( [id://1036299]=note: print w/replies, xml ) | Need Help?? |
An Easy WayThere are a few simple ways of doing it. You could just have a method that calls the attributes that you want to search, something like this:
Which would return something like:
But that's no fun! You'd have to change that method every time you changed your Note class. Plus, this is an excellent chance to do something amusing with Moose attributes! What if you could just mark an attribute as searchable, then have a method that goes through and searches those attributes? A Harder WayWell, you could define a 'Searchable' trait, like so:
Then have a role that uses that trait to search through your attributes, like so:
Then finally, in your class, you mark the traits you want as searchable:
Then this code returns the same result: ...returns...
There's a lot more up-front work, and it's probably overkill for what you want to do. Still, now you can add and delete new attributes as you please, and they'll be searched properly so long as they're marked searchable! stephen
In Section
Seekers of Perl Wisdom
|
|