In my own code I use my own method maker module (not on CPAN because there are already enough method makers on CPAN) that does something similar but slightly unusual. Doing
will create a setBar and getBar pair of methods and also a Bar lvalue method which just calls setBar and getBar. So far, so normal. What's unusual is that it puts the methods into Foo::Methods and makes Foo inherit from Foo::Methods so that if I want to have something special (validation etc) happen when a setter or getter is called. I just dopackage Foo; use Fergal::MM qw( Bar );
Yes, there's plenty of overhead in this but usually it doen't make any difference to me.package Foo; use Fergal::MM qw( Bar ); sub setBar { my $self = shift; my $value = shift; check($value); $self->SUPER::setBar($value); }
Another interesting thing I do with lvalue accessors is to add indexed accessors. So I can do something like
this gets translated into$a->Grid(2, 3) = $player1; $a->Grid(7, 5) = $player2;
so by writing the correct setters and getters, you can create attributes that act like multidimensional arrays or hashes and that are also overrideable by sub classes and can have validation etc.$a->setGrid($player1, 2, 3); $a->setGrid($player2, 7, 5);
In reply to Re^3: Are lvalue methods (as properties) a good idea?
by fergal
in thread Are lvalue methods (as properties) a good idea?
by jplindstrom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |