NAME

MooseX::Role::Parameterized::Tutorial - why and how

MOTIVATION

Roles are composable units of behavior. They are useful for factoring out functionality common to many classes from any part of your class hierarchy. See Moose::Cookbook::Roles::Recipe1 for an introduction to Moose::Role.

While combining roles affords you a great deal of flexibility, individual roles have very little in the way of configurability. Core Moose provides alias for renaming methods and excludes for ignoring methods. These options are primarily (perhaps solely) for disambiguating role conflicts. See Moose::Cookbook::Roles::Recipe2 for more about alias and excludes.

Because roles serve many different masters, they usually provide only the least common denominator of functionality. To empower roles further, more configurability than alias and excludes is required. Perhaps your role needs to know which method to call when it is done. Or what default value to use for its url attribute.

Parameterized roles offer exactly this solution.

USAGE

with

The syntax of a class consuming a parameterized role has not changed from the standard with. You pass in parameters just like you pass in alias and excludes to ordinary roles:

with 'MyRole::InstrumentMethod' => {
    method_name => 'dbh_do',
    log_to      => 'query.log',
};

You can still combine parameterized roles. You just need to specify parameters immediately after the role they belong to:

with (
    'My::Parameterized::Role' => {
        needs_better_example => 1,
    },
    'My::Other::Role',
);

parameter

Inside your parameterized role, you specify a set of parameters. This is exactly like specifying the attributes of a class. Instead of has you use the keyword parameter, but your parameters can use any options to has.

parameter 'delegation' => (
    isa       => 'HashRef|ArrayRef|RegexpRef',
    predicate => 'has_delegation',
);

You do have to declare what parameters you accept, just like you have to declare what attributes you accept for regular Moose objects.

role

role takes a block of code that will be used to generate your role with its parameters bound. Here is where you declare parameterized components: use has, method modifiers, and so on. The role block receives an argument, which contains the parameters specified by with. You can access the parameters just like regular attributes on that object.

Each time you compose this parameterized role, the role {} block will be executed. It will receive a new parameter object and produce an entirely new role. That's the whole point, after all.

Due to limitations inherent in Perl, you must declare methods with method name => sub { ... } instead of the usual sub name { ... }. Your methods may, of course, close over the parameter object. This means that your methods may use parameters however they wish!

USES

Ideally these will become fully-explained examples in something resembling Moose::Cookbook. But for now, only a braindump.

Configure a role's attributes

You can rename methods with core Moose, but now you can rename attributes. You can now also choose type, default value, whether it's required, traits, etc.

parameter traits => (
    isa     => 'ArrayRef[Str]',
    default => sub { [] },
);

parameter type => (
    isa     => 'Str',
    default => 'Any',
);

role {
    my $p = shift;

    has action => (
        traits => $p->traits,
        isa    => $p->type,
        ...
    );
}
Inform a role of your class' attributes and methods

Core roles can require only methods with specific names. Now your roles can require that you specify a method name you wish the role to instrument, or which attributes to dump to a file.

parameter instrument_method => (
    isa      => 'Str',
    required => 1,
);

role {
    my $p = shift;
    around $p->instrument_method => sub { ... };
}
Arbitrary execution choices

Your role may be able to provide configuration in how the role's methods operate. For example, you can tell the role whether to save intermediate states.

parameter save_intermediate => (
    isa     => 'Bool',
    default => 0,
);

role {
    my $p = shift;
    method process => sub {
        ...
        if ($p->save_intermediate) { ... }
        ...
    };
}
Deciding a backend

Your role may be able to freeze and thaw your instances using YAML, JSON, Storable. Which backend to use can be a parameter.

parameter format => (
    isa     => (enum ['Storable', 'YAML', 'JSON']),
    default => 'Storable',
);

role {
    my $p = shift;
    if ($p->format eq 'Storable') {
        method freeze => \&Storable::freeze;
        method thaw   => \&Storable::thaw;
    }
    elsif ($p->format eq 'YAML') {
        method freeze => \&YAML::Dump;
        method thaw   => \&YAML::Load;
    }
    ...
}