Salvador Fandiño García > mcoder-0.10 > mcoder

Download:
mcoder-0.10.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.10   Source  

NAME ^

mcoder - perl method generator from common templates

SYNOPSIS ^

  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 }

ABSTRACT ^

generate common templated methods like accessors, proxies, etc.

DESCRIPTION ^

mcoder usage is:

  use mcoder $type1 => $arg1, $type2 => $arg2, ...;
  use mcoder [$type11, $type12, $type13,...] => $arg1, ...;

where $type/$arg pairs can be:

get
  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.

set
  use mcoder set => $name;
  use mcoder set => { $name1 => $attr1, $name2 => $attr2, ... };
  use mcoder set => [$name1, $name2, $name3, ...];

generate write accessors named as set_$name.

calculated
  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.

array_calculated

similar to calculated but caches an array of values instead of a single value.

proxy
  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

delete
  use mcoder delete => $name;
  use mcoder delete => { $name1 => $attr1, $name2 => $attr2, ... };
  use mcoder delete => [$name1, $name2, $name3, ...];
undef
  use mcoder undef => $name;
  use mcoder undef => { $name1 => $attr1, $name2 => $attr2, ... };
  use mcoder undef => [$name1, $name2, $name3, ...];
new
  use mcoder new => $name;

generates a simple constructor for a hash based object

bool_set
bool_unset
  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

virtual
  use mcoder virtual => $name;
  use mcoder virtual => [$name, ...];

the method throws an error when called.

EXPORT

whatever you ask ;-)

ALTERNATIVES FROM CPAN ^

Somebody asked me why he should prefer mcoder over any other similar package from CPAN. Well, there are mostly two reasons:

1 - a matter of style

mcoder generates methods as I use to, following my personal style:

- different methods for read/write access, i.e. slot/set_slot
- boolean properties have set/unset methods that don't take arguments at all.
- calculated (or cached or lazy) attributes.

(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)

2 - a short name and simple syntax

"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:

SEE ALSO ^

mcoder::set, mcoder::get, mcoder::calculated, mcoder::proxy are syntactic sugar for this module.

Class::MethodMaker has a similar functionality.

AUTHOR ^

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

COPYRIGHT AND LICENSE ^

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.

syntax highlighting: