Comments on Parameterized Roles with MooseX::Declarehttp://perldition.org/This is Florian Ragwitz' blogAngerwhale2011-11-03T09:36:02ZParameterized Roles with MooseX::DeclareFlorian Ragwitzrafl@fsfe.orgurn:guid:a9b71ca9-4050-4322-9afe-2f5e2ee9a7762009-08-19T08:27:25Z

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::DeclareAnonymous Cowardurn:guid:266fc7cb-43c9-413e-bdb4-59642603cfa12011-06-17T13:46:20Z
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:Anonymous Cowardurn:guid:d1c6c673-7b25-45cb-b110-79a5ab96c3922011-08-28T06:59:39Z

freelance writer

Re:Anonymous Cowardurn:guid:d1c6c673-7b25-45cb-b110-79a5ab96c3922011-08-28T06:59:39Z

freelance writer