in reply to Re^2: Are lvalue methods (as properties) a good idea?
in thread Are lvalue methods (as properties) a good idea?

Class::Accessor::Lvalue also allows you to create them for a class and it has the nice feature that a call to $a->wibble = 5 gets translated to $a->set_wibble(5) so all your usual verification code is still callable (or you can choose to autogenerate a plain old setter with Class::Accessor).

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

package Foo; use Fergal::MM qw( Bar );
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 do
package Foo; use Fergal::MM qw( Bar ); sub setBar { my $self = shift; my $value = shift; check($value); $self->SUPER::setBar($value); }
Yes, there's plenty of overhead in this but usually it doen't make any difference to me.

Another interesting thing I do with lvalue accessors is to add indexed accessors. So I can do something like

$a->Grid(2, 3) = $player1; $a->Grid(7, 5) = $player2;
this gets translated into
$a->setGrid($player1, 2, 3); $a->setGrid($player2, 7, 5);
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.

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

    That is very cool. If you don't want to put this on CPAN, maybe you could talk to the author(s) of (an) existing module(s) and see if they wouldn't like to support for such?

    Makeshifts last the longest.

      The main reason for not putting it on CPAN is that I don't want to have to try and support other people's favourite method making features. I've been meaning to try get it into the one of the other packages but I've been away from home for a while and changing jobs etc. I might publish it as a proof of concept and invite others to steal any ideas that they like.

        I can see that. How about you put it on CPAN as a developer release and never pull off the tag? Others might be more motivated to steal ideas if they have a working model.

        Makeshifts last the longest.