Mouse/Moose delegation feature

Using Mouse for my experimental Module Forward::ORM really seems to pay off.

One of my classes just has to delegate method calls to another class, here my first draft:

##  Delegation (using Method::Signatures::Simple)
method manager {Forward::ORM::Migrations::Manager->new}
method create_table (@params) {$self->manager->create_table(@params)}
method add_column (@params) {$self->manager->add_column(@params)}
method remove_column (@params) {$self->manager->remove_column(@params)}

with the Mouse handles command, it becomes:

has manager => (
    default => sub {Forward::ORM::Migrations::Manager->new},
    handles => [qw/add_column create_table remove_column/]

This is 46% less code. And the savings will become even bigger as I will add new commands :)

The commit


If you would have fixed your @ISA no code at would have been needed. Using Mouse or Moose for this is like shooting with nuclear grenades on birds.

The method_named op searches all the ISA classes for the first method.

Reini: Sure, if you're okay with exposing the entire API of the other module through your class, and are okay with having no way at all of dealing with method name conflicts (or even knowing when they happen). This is rarely a good design - multiple inheritance is almost always a mistake. Delegation and/or roles pretty much always leads to a cleaner design.

Leave a comment

About Forward Ever

user-pic I blog about Perl.