November 2011 Archives

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

Ruby like code blocks in Perl

While thinking about how to best define migrations/tables in my experimental module Forward::ORM, I realized that I might need Ruby like code blocks to keep certain things in scope:

In my first version, I used default Perl:

sub up {
    my $self = shift;

    $self->create_table('authors', sub {my $t = shift;
        $t->add_column('id', 'integer', primary_key => 1);
        $t->add_column('name', 'varchar', length => 40);
    });
}

I then noticed that Method::Signatures::Simple also works with anonymous functions and methods, so my code now looks like this:

sub up {
    my $self = shift;

    $self->create_table('authors', func($t){
        $t->add_column('id', 'integer', primary_key => 1);
        $t->add_column('name', 'varchar', length => 40);
    });
}

and later

$code_ref->($table);

While not as pretty as Ruby code blocks, as func has to be passed as a parameter and thus must be surrounded by braces, it is at least a small improvement.

search.cpan.org down

Today, and in recent weeks, search.cpan.org has been down a couple of times, but NOT really:

Turning off my router and then turning it on again actually helps!

Does that make any sense at all???

By the way, search.cpan.org is the only site showing this strange behaviour!

About Forward Ever

user-pic I blog about Perl.