in reply to Re^2: Are lvalue methods (as properties) a good idea?
in thread Are lvalue methods (as properties) a good idea?
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);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Are lvalue methods (as properties) a good idea?
by Aristotle (Chancellor) on Jan 18, 2005 at 08:27 UTC | |
by fergal (Chaplain) on Jan 18, 2005 at 16:01 UTC | |
by Aristotle (Chancellor) on Jan 18, 2005 at 19:39 UTC | |
by fergal (Chaplain) on Jan 18, 2005 at 21:00 UTC | |
by Aristotle (Chancellor) on Jan 18, 2005 at 21:13 UTC |