LanX has asked for the wisdom of the Perl Monks concerning the following question:
In OOP it really bothers me to write use getter/setters instead of lvalues for simple attributes like in other languages.
As shown in PBP, the problem with lvalue is, that if ever you need a wrapper to control the setting of the attribute you can only rely on tieing the variable or you have to change the API and refactor...
Which is not really an issue of ugly syntax, I found at least two cpan-modules showing that a posteriori FETCH and STORE can be done in a very clean, readable and maintainable syntax. e.g. in Class::Closure
I did some benchmarks showing that accessing a tied variable via lvalue is 4-5 times slower than with a getter sub! : (
Now I "superfound" Re^3: Class::Accessor and Damian's PBP and was quite exited to see Contextual::Return...
So, very curious about how TheDamian fixed the problem, I took a look into the source:
$impl = tie $_[0], 'Contextual::Return::Lvalue', $subname => $handler, args=>$args;
Do I get it right, it's just another sugar for Tieing? 8 (
Well I don't know how big the trade-off of wrapping lvalues is in other languages ...
But shouldn't this general "banning" of lvalues better be replaced by the rule
It's acceptable to use them as long you are aware that later changing will result in a call-overhead of 400%!Time critical calls should be done with getter/setters!
???
BTW: Is there any activity to speed up tie? I don't see why the calling mechanism for FETCH and STORE needs to undergo the same slow dynamic ways OOP needs. A kind of "tie :static" might really solve the problem!
Hopefully looking for a serious discussion of this somehow "religious" matter..
Cheers LanX
- - - - - Which song???
|
---|