
mcoder - perl method generator from common templates

package MyClass;
use mcoder [qw(get set)] => [qw(color sound height)], \
proxy => [qw(runner run walk stop)], \
calculated => weight;
sub _calculate_weight { shift->ask_weight }

generate common templated methods like accessors, proxies, etc.

mcoder usage is:
use mcoder $type1 => $arg1, $type2 => $arg2, ...; use mcoder [$type11, $type12, $type13,...] => $arg1, ...;
where $type/$arg pairs can be:
use mcoder get => $name;
use mcoder get => { $name1 => $attr1, $name2 => $attr2, ... };
use mcoder get => [$name1, $name2, $name3, ...];
generate read accessors that returns the value in $self->{$name} or $self->{$attr} or $self->$attr.
use mcoder set => $name;
use mcoder set => { $name1 => $attr1, $name2 => $attr2, ... };
use mcoder set => [$name1, $name2, $name3, ...];
generate write accessors named as set_$name.
use mcoder calculated => $name;
use mcoder calculated => { $name1 => $attr1, $name2 => $attr2, ... };
use mcoder calculated => [$name1, $name2, $name3, ...];
similar to read accessors (set) but when the value is unexistant, method _calculate_$name is called and its result cached.
similar to calculated but caches an array of values instead of a single value.
use mcoder proxy => [$delegate, $name1, $name2, $name3];
use mcoder proxy => [$delegate, { $name1 => $del_method1,
$name2 => $del_method2, ... } ];
forward method calls to $self->$delegate->$del_method
use mcoder delete => $name;
use mcoder delete => { $name1 => $attr1, $name2 => $attr2, ... };
use mcoder delete => [$name1, $name2, $name3, ...];
use mcoder undef => $name;
use mcoder undef => { $name1 => $attr1, $name2 => $attr2, ... };
use mcoder undef => [$name1, $name2, $name3, ...];
use mcoder new => $name;
generates a simple constructor for a hash based object
use mcoder bool_unset => $name;
use mcoder bool_unset => { $name1 => $attr1, $name2 => $attr2, ... };
use mcoder bool_unset => [$name1, $name2, $name3, ...];
use mcoder bool_unset => $name;
use mcoder bool_unset => { $name1 => $attr1, $name2 => $attr2, ... };
use mcoder bool_unset => [$name1, $name2, $name3, ...];
generates methods that set or unset a boolean property
use mcoder virtual => $name; use mcoder virtual => [$name, ...];
the method throws an error when called.
whatever you ask ;-)

Somebody asked me why he should prefer mcoder over any other similar package from CPAN. Well, there are mostly two reasons:
mcoder generates methods as I use to, following my personal style:
slot/set_slot(mcoder is the result of a big application cleanup when at some point I notice that I was coding too many simple methods always the same. That's also why I didn't use any other module from CPAN: I need the generated methods to be drop in replacements for the old ones that I had manually coded)
"mcoder" is a pretty short name, and you would like it even if it is a little cryptic.
The "sugar" sub-modules allow for a simpler syntax, i.e.:
use mcoder::proxy sleeper => qw(weakup sleep);
Other similar modules from CPAN that you would like to considered are:
This module is able to generate everything you would ever need!
This module is too complex for what it does. In my opinion it doesn't worth the trouble of learning to use it.
-
-
very simple, and has yet another different style for accessors!

mcoder::set, mcoder::get, mcoder::calculated, mcoder::proxy are syntactic sugar for this module.
Class::MethodMaker has a similar functionality.

Salvador Fandiño, <sfandino@yahoo.com>

Copyright 2003 by Salvador Fandiño
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.