On Wed, May 06, 2009 at 01:34:50PM -0400, Dmitri wrote:
> Is there a clean way to share data from class in a role?

use MooseX::Role::Parameterized

> If it was an object method I would probably set up attribute for that,  
> but it's a class method.

perfect:

  package FN::Utils;
  use MooseX::Role::Parameterized;
  use MooseX::Types::Moose qw(Str);

  parameter table => (isa => Str, required => 1);

  role {
    my $p = shift;
    my $table = $p->table;
    method some_db_op => sub { ... do something with $table ... };
  }

  package FN::User;

  use Moose;
  with 'FN::Utils' => { table => 'fnuser' };

Only downside: you can't compose parameterized roles with other roles.  This is
a known bug, and we're planning on fixing it at YAPC if we haven't done it by
then.

hdp.

Reply via email to