Parameterized Roles with MooseX::Declare

Posted on 2009-8-19 (水) at 10:27 am by Florian Ragwitz [SIGNED]

Posted in: Perl

You've probably already heard of Sartak's awesome MooseX::Role::Parameterized.

As of version 0.25, MooseX::Declare provides some nice sugar for that. This is what it looks like:

use MooseX::Declare;

role Counter (Str :$name) {
  has $name => (is => 'rw', isa => 'Int', default => 0);

  method "increment_${name}" {
    $self->$name( $self->$name + 1 );

  method "reset_${name}" {

class MyGame::Weapon {
  with Counter => { name => 'enchantment' };

class MyGame::Wand {
  with Counter => { name => 'zapped' };

Just thought I'd let you know, since i haven't gotten around to actually document this. In case you like it, have some spare tuits, and want to help out, the repository is right here :-)

3 comments |

MongoDB on CPAN

Posted on 2009-5-9 (土) at 12:51 am by Florian Ragwitz [SIGNED]

Posted in: Perl

I've been doing some contracting work for 10gen recently. They have that rather cool open source document database called MongoDB and they wanted me to write a module to use that from Perl. I did that and the code is now available on CPAN and github.

Writing that was fun, and I'm already looking forward to be able to use MongoDB as a backend for KiokuDB. I started writing code for that and put it on github, but isn't passing all the tests just yet.

In related news, after finishing the MongoDB module, I'm available for other things again. So if you're looking for a Perl telecommuter, let me know.

no comments |

Last modified: 2009-5-9 (土) at 12:53 am

Running tests that require an X server

Posted on 2009-4-29 (水) at 5:20 pm by Florian Ragwitz [SIGNED]

Posted in: Perl

Lots of CPAN distributions require some kind of graphical environment. Some of them even pop up windows, which not only very annoying, but also sometimes fails if you're using a tiled window manager.

To test such distributions on a machine where no graphical environment is available or on your desktop while you're working and don't want to get annoyed to death you can use a fake X server, like Xvfb.

The easiest way to do that is to run

$ xvfb-run -a make test

instead of a plain make test. That'll automatically create a fake xserver, set up DISPLAY and run make test in that environment.

That works well for manually installing modules. When installing using you can make things easier by writing a distropref.

First, tell cpan where your distroprefs are. I use ~/.cpan/prefs:

$ cpan
cpan[1]> o conf init prefs_dir
Directory where to store default options/environment/dialogs for
building modules that need some customization? [] /home/rafl/.cpan/prefs
cpan[3]> o conf commit
commit: wrote '/home/rafl/.cpan/CPAN/'

Now write a distropref for the modules that need an X server and put it into your prefs dir as X11.yml

  distribution: |
      |... other modules requiring an X server
  commandline: "xvfb-run -a make test"

Now the tests for Wx, Gtk2, Gnome2 and all other distributions you list in that regex will be executed with a fake X server.

I have yet to figure out how to write a distropref that just prepends to the test commandline instead of replacing it so I won't need to have another pref for all modules using Module::Build.

4 comments |

Last modified: 2009-4-29 (水) at 5:22 pm

Implementing Typed Lexical Variables

Posted on 2009-4-28 (火) at 8:09 am by Florian Ragwitz [SIGNED]

Posted in: Perl

For quite some time perl provided a form of my declarations that includes a type name, like this:

my Str $x = 'foo';

However, that didn't do anything useful, until Vincent Pit came along and wrote the excellent Lexical::Types module, which allows you to extend the semantics of typed lexicals and actually make them do something useful. For that, it simply invokes a callback for every my declaration with a type in the scopes it is loaded. Within that callback you get the variable that is being declared as well as the name of the type used in the declaration.

We also have Moose type constraints and the great MooseX::Types module, that allows us to define our own type libraries and import the type constraints into other modules.

Let's glue those modules together. Consider this code:

use MooseX::Types::Moose qw/Int/;
use Lexical::Types;
my Int $x = 42;

The first problem is that the perl compiler expects a package with the name of the type used in my to exist. If there's no such package compilation will fail.

Creating top-level namespaces for all the types we want to use would obviously suck. Luckily the compiler will also try to look for a function with the name of the type in the current scope. If that exists and is inlineable, it will call that function and use the return value as a package name.

In the above code snippet an Int function already exists. We imported that from MooseX::Types::Moose. Unfortunately it isn't inlineable. Even if it were, compilation would still fail, because it would return a Moose::Meta::TypeConstraint instead of a valid package name.

To fix that, let's rewrite the code to this:

use MooseX::Types::Moose qw/Int/;
use MooseX::Lexical::Types qw/Int/;
my Int $x = 42;

Let's also write a MooseX::Lexical::Types module that replaces existing imported type exports with something that can be inlined and returns an existing package name based on the type constraint's name.

package MooseX::Lexical::Types;

use Class::MOP;
use MooseX::Types::Util qw/has_available_type_export/;
use namespace::autoclean;

sub import {
    my ($class, @args) = @_;
    my $caller = caller();

    my $meta = Class::MOP::class_of($caller) || Class::MOP::Class->initialize($caller);

    for my $type_name (@args) {
        # get the type constraint by introspecting the caller
        my $type_constraint = has_available_type_export($caller, $type_name);

        my $package = 'MooseX::Lexical::Types::TYPE::' . $type_constraint->name;
        $meta->add_package_symbol('&'.$type_name => sub () { $package });

    Lexical::Types->import; # enable Lexical::Types for the caller


With that the example code now compiles. Unfortunately it breaks every other usecase of MooseX::Types. The export will still need to return a Moose::Meta::TypeConstraint at run time so this will continue to work:

has some_attribute => (is => 'ro', isa => Int);

So instead of returning a plain package name from our exported function we will return an object that delegates all method calls to the actual type constraint, but evaluates to our special package name when used as a string:

my $decorator = MooseX::Lexical::Types::TypeDecorator->new($type_constraint);
$meta->add_package_symbol('&'.$type_name => sub () { $decorator });


package MooseX::Lexical::Types::TypeDecorator;
use Moose;
use namespace::autoclean;

# MooseX::Types happens to already have a class that doesn't do much
# more than delegating to a real type constraint!
extends 'MooseX::Types::TypeDecorator';

use overload '""' => sub {
    'MooseX::Lexical::Types::TYPE::' . $_[0]->__type_constraint->name


Now we're able to use Int as usual and have Lexical::Types invoke its callback on MooseX::Lexical::Types::TYPE::Int. Within that callback we will need the real type constraint again, but as it is invoked as a class method with no good way to pass in additional arguments, we will need to store the type constraint somewhere. I choose to simply add a method to the type class we create when constructing our export. After that, all we need is to implement our Lexical::Types callback. We will put that in a class all our type classes will inherit from:

    $package => (
        superclasses => ['MooseX::Lexical::Types::TypedScalar'],
        methods      => {
            get_type_constraint => sub { $type_constraint },

The Lexical::Types callback will now need to tie things together by modifying the declared variable so it will automatically validate values against the type constraint when being assigned to. There are several ways of doing this. Using tie on the declared variable would probable be the easiest thing to do. However, I decided to use Variable::Magic (also written by Vincent Pit - did I mention he's awesome?), because it's mostly invisible at the perl level and also performs rather well (not that it'd matter, given that validation itself is relatively slow):

package MooseX::Lexical::Types::TypedScalar;

use Carp qw/confess/;
use Variable::Magic qw/wizard cast/;
use namespace::autoclean;

my $wiz = wizard
    # store the type constraint in the data attached to the magic
    data => sub { $_[1]->get_type_constraint },
    # when assigning to the variable, fail if we can't validate the
    # new value ($_[0]) against the type constraint ($_[1])
    set  => sub {
        if (defined (my $msg = $_[1]->validate(${ $_[0] }))) {
            confess $msg;

    # cast $wiz on the variable in $_[1]. pass the type package name
    # in $_[0] to the wizard's data construction callback.
    cast $_[1], $wiz, $_[0];


With this, our example code now works. If someone wants to assign, say, 'foo' to the variable declared as my Int $x our magic callback will be invoked, try to validate the value against the type constraint and fail loudly. WIN!

The code for all this is available github and should also be on CPAN shortly.

You might notice warnings about mismatching prototypes. Those are caused by Class::MOP and fixed in the git version of it, so they'll go away with the next release.

There's still a couple of caveats, but please see the documentation for that.

3 comments |

Last modified: 2009-4-28 (火) at 8:20 am

Declaring Catalyst Actions

Posted on 2009-4-26 (日) at 10:13 am by Florian Ragwitz [SIGNED]

Posted in: Perl

For a long time the Catalyst Framework has been using code attributes to allow users to declare actions that certain URLs get dispatched to. That looks something like this:

sub base    : Chained('/')    PathPart('') CaptureArgs(0) { ... }
sub index   : Chained('base') PathPart('') Args(0)        { ... }
sub default : Chained('base') PathPart('') Args           { ... }

It's a nice and clean syntax that keeps all important information right next to the method it belongs to.

However, attributes in perl have a couple of limitations. For one, the interface the perl core provides to use them is horrible and doesn't provide nearly enough information to do a lot of things, but most importantly attributes are just plain strings. That means you will need to parse something like "Chained('base')" into (Chained => 'base') yourself to make proper use of them.

While that's easy for the above example, it can be very hard in the general case because only perl can parse Perl. It's one of the reasons you can't use Catalyst::Controller::ActionRole to apply parameterized roles to your action instances, because parsing parameters out of things like Does(SomeRole => { names => [qw/affe tiger/], answer_re => qr/42/ }) would be awful and wrong.

With Catalyst 5.8 most of the attribute related code has been removed from the internals. It's now using MooseX::MethodAttributes to do all the heavy lifting. Also the internals of how actions are registered have been refactored to make it easier to implement alternate ways without changing the Catalyst core.

As a proof of concept for this I implemented a new way of declaring actions that's very similar to how Moose provides it's sugar functions. You can get it from github.

With that, the above example looks like this:

action base    => (Chained => '/',    PathPart => '', CaptureArgs => 0) => sub { ... };
action index   => (Chained => 'base', PathPart => '', Args    => 0    ) => sub { ... };
action default => (Chained => 'base', PathPart => '', Args    => undef) => sub { ... };

It also moves method declaration from compiletime to runtime, making this possible:

for my $action (qw/foo bar baz/) {
    action $action => (Chained => 'somewhere', Args => 0) => sub {
        my ($self, $ctx) = @_;
        $ctx->stash->{ $action } = $ctx->model('Foo')->get_stuff($action);

Admittedly, that's all very ugly, but illustrates well what kind of things we're able to do now. But it doesn't need to be ugly. With Devel::Declare we have a great tool to add our own awesome syntax to perl, similar to how things like MooseX::Method::Signatures, MooseX::MultiMethods and MooseX::Declare do.

So how would a declarative syntax for Catalyst controllers look like? I don't know. Ideas include something like this:

under /some/where, action foo ('foo', $id) { ... }

to mean:

sub foo : Chained('/some/where') PathPart('foo') CaptureArgs(1) { ... }

Adding Moose type constraints to this would be interesting, too, and make validation of captures and arguments a lot easier. Multi dispatch similar to MooseX::MultiMethods could be handy as well:

under /some/where {
    action ('foo', Int $id) {
        # find and stash an item by id
    action ('foo', Str $name) {
        # search items using $name
    action ('foo', Any $thing) {
        # display error page

So you see there are a lot of possibilities that should be explored. Unfortunately I have no idea what kind of syntax and features people would like to have, so your feedback on this would be much appreciated. :-)

2 comments |

Last modified: 2009-4-26 (日) at 11:14 pm