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}" {
    $self->$name(0);
  }
}

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




Navigation

Categories

Tags

Login