Thoughts on parametrized roles for Moose

July 15th, 2008

Moose is your favorite meta-object system in your favorite language of choice. And you already have a lot of excellent concepts that extend the basic usage of object-oriented programming, like having simple means to override parts of methods (around, before, after) and of course roles.

Roles are great, I even used them for my logging needs in MooseX::Log::Log4perl as stated earlier. After getting getting feedback by Micheal Schilli to add an easier interface for simple logging needs I had two choices to accomplish that:

  • Add another role (and reuse the initial role) like it is implemented currently in MooseX::Log::Log4perl::Easy:

    package MooseX::Log::Log4perl::Easy;
    use Moose::Role; ### Make it a moose role
    with 'MooseX::Log::Log4perl'; ### Reuse the base role with its attributes and methods
    sub log_fatal { my $self = shift; $self->logger->fatal(@_); }
    sub log_error { my $self = shift; $self->logger->error(@_); }
  • Use a method alias by using import to have a function returning the correct role to use with with. This is exactly what MooseX::Storage does to allow parametrized loading of moose roles to save some typing for lazy people and improving readability.

    use Moose;
    use MooseX::Storage; ### You have to use it to allow import to provide you the Storage alias
    with Storage('format' => 'JSON', 'io' => 'File'); ### Use the function to return the correct roles

The first approch clutters my module package a little, also requiring more documentation and hinting for people to find the module (more of a problem for lazy people like me).
The downside of the second approach is, that you have to use MooseX::Storage first, to have the Storage function exported. And it does not really look like the standard way of adding a role to the object, which is usually defined using quoted string like with 'MooseX::Log::Log4perl'; (note the quotes here).
A solution to that problem might be adding another keyword function to moose e.g. called role that makes use of some import magic and returns the correct role packages to load, also calling role initialization method, that allows to do some role tricks.

use Moose;
with role 'My::Role'; ### no magic here same as: with 'My::Role';
with role 'MooseX::Log::Log4perl' => ':easy'; ### pass a param
### or even
with role 'MooseX::Log::Log4perl', prefix => 'mylog_'; ### pass the param hash/pair to a role

The role keyword would return the correct role method to load, and additionally allow the role to initialize and use the prefix parameter to do some additional initialization, even if that would mean only setting a “_role_param attribute” that could be used later in default coderefs. Using this, would mean that you cannot use multiple roles with the with role keywords, but that’s ok for me, since perl users are used to that anyway, it’s the same for use.

Bringing logging to Moose with MooseX::Log::Log4perl

July 13th, 2008

Finally after some playing around and discovering the main concepts behind moose and failing to find a logging role using my favorite logging system log4perl I sat down an did a little coding (it is really just a few lines) and uploaded to CPAN as MooseX::Log::Log4perl.

As I already received some valuable feedback by Michael Schilli of Log4perl fame, the interface might change a little (staying backwards compatible) to be easier to use for small projects by using something like with MooseX::Log::Log4perl qw(:easy) to give you methods for log_error, log_warn, log_debug, … directly on your class instance.

If your interested see the open RT ticket 37655.

This module was actually written as part of my other project called QuikPlan, a course scheduling and management web application.

Quikplan – Ein neues Catalyst Perl / jquery javascript Projekt

July 4th, 2008

Mein neues Projekt Quikplan geht an den Start. Gemeinsam mit Chris entsteht eine Kursplanungssoftware die speziell auf die Anforderungen einer Kletterhalle (oder ähnlichem) getrimmt ist.

Als Backend dient Catalyst Perl mit diversen weiteren Modulen (Moose, DBIx::Class, …) getrennt über ein JSON basierendes API. Das Frontend ist eine Web 2.0 Applikation basierend auf jQuery und einen eigens dafür erstellten Framework für einfaches und konsistentes GUI Handling.

Die ersten Erfahrungen mit Moose sind sehr vielversprechend und die erste Klasse (ein TransactionManager als Zwischenlayer zu den DBIC/DBI Transaktionen) ist bereits erstellt.

Für mehr Informationen: bzw. ein Mail auf die Reise schicken (info A T quikplan D O T at).

Somis Milestone 2 reached…

November 17th, 2005

Milestone 2 has been approved (altough one document was missing, but noone noticed).

We started hacking on the catalyst base now, mostly just some basic controller and view parts.

UML and ERD are nearly finished, so we seem to be on schedule, that’s fine!

Chris even created some first sketches for a logo for Somis.

Somis Milestone 1 reached…

November 3rd, 2005

Milestone 1 has been submited and we received positive feedback already, so we can continue on reaching milestone 2. Coding will start as soon as all documents for milestone 2 (QB2) are in place.

Looking fine so far, we are still on schedule and still quite motivated, I’ll be the one trying to keep it that way.

Approching first Somis milestone

October 14th, 2005

We have been hard at work, to finish in time, for our first milestone, which was targeted at Oct. 15. But thanks to our project tutor, who informed us recently, that he won’t be available til Oct. 27th. Good thing is that it seems to actually work out as the usual projects in real life…

We will continue working on the second milestone (QB2), to keep the pace up we have now. The milestones themselves will be slightly adjusted (move QB1 two weeks, QB2 one week, and we’re again on schedule.

The project name itself will remain “WWS-On”, while the working title for the software prototype will be “Somis”.

So let’s see how the concepual milestones work out, implementation will be probably quite straightforward, as soon as the Catalyst framework is understood.

Next »