|Perl Monk, Perl Meditation|
Some thoughts on Moose Attributesby John M. Dlugosz (Monsignor)
|on May 01, 2011 at 13:22 UTC||Need Help??|
John M. Dlugosz has asked for the
wisdom of the Perl Monks concerning the following question:
In my first real code using Moose, I noticed some things about attributes.
Basically, it is not nice to have to call functions to get and to set each time. I noticed later that some use of "native traits" delegation can mitigate that, but not completely, as it would lead to a proliferation of specific methods; but that's an issue really because Perl built-in types like arrays are not themselves accessed via method syntax and built-in functions don't take references.
The real issue, that can't be put off on other things still needing to catch up, concern very simple types like plain numbers and strings.
Or perhaps$self->input_line_number(1+$self->input_line_number);. But, recall that the ++ syntax is beloved by millions and for a reason! It is quite a bit out of the way to not be able to manipulate things in-place.
Now in Perl6, the accessors return lvalues, so something like ++$self->input_line_number would work just fine.
So here is a specific question: why not have an lvalue accessor in Moose? That could be enabled on request, or the default when it is declared to be a non-reference type.
More generally, how are people coping with the large affordance distance between the method's code and the per-class instance data? It's always been large in Perl 5, but at least I could refer to the slots (via hash access) as lvalues. It seems to be a chore, all the time, for code to get to its values. In C++ you just say x in scope; in Moose you have to say $self->x twice, to get and again to set, and work on a copy.