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




  • Re: Parameterized Roles with MooseX::Declare

    Written by Anonymous Coward (0) on 2011-6-17 (金) at 3:46 pm

    Just one clarification - in the examples above: return $p->prefix .":". $self->$orig($string); is later in the MooseX; ::Declare version transformed into: return $self->$orig("$prefix: $string"); does that mean that the MooseX::Declare peeks into the parameter and parses it to remove $prefix: and them attach it to the output of $self->$orig($string); ?
  • Re:

    Written by Anonymous Coward (0) on 2011-8-27 (土) at 8:13 am

    freelance writer

  • Re:

    Written by Anonymous Coward (0) on 2011-9-8 (木) at 1:03 am

    freelance writer


Navigation

Categories

Tags

Login