P is for Practical | |
PerlMonks |
Moose Trait to Add a Method to an Attributeby choroba (Cardinal) |
on Oct 07, 2016 at 20:36 UTC ( [id://1173508]=perlquestion: print w/replies, xml ) | Need Help?? |
choroba has asked for the wisdom of the Perl Monks concerning the following question:
Dear brethren and sistren! Imagine a Moose class with an array attribute.
If we need to change all the elements of the attribute to uppercase, the simplest solution would be to use map directly:
Now, suppose our attribute is more complex. We'll implement it with the Array trait and its map method:
Which changes the code to
We can still imagine a simpler interface: the apply_to_list method that takes a code reference and applies it to each element of the list. I was able to implement it as another trait:
But that's not exactly what we imagined. Is it possible to create a trait (or modify the Array trait) in a way that it adds a new apply method to the attribute which we can use in delegation, i.e. something like
I've been trying for several hours, but I just don't know how to do it. Do you?
Back to
Seekers of Perl Wisdom
|
|