---
name: "Moose::Cookbook::Recipe11"
section: "3pm"
platform: "linux"
package: "libmoose-perl"
version: "0.54-1"
release: "Debian 5.0 (Lenny)"
platforms: ["linux"]
url: "https://new.carta.tech/Moose::Cookbook::Recipe11"
---

# Moose::Cookbook::Recipe11(3pm)

Advanced Role Composition - method exclusion and aliasing

## NAME

Moose::Cookbook::Recipe11 - Advanced Role Composition - method exclusion and aliasing

## SYNOPSIS

```
  package Restartable;
  use Moose::Role;
  has 'is_paused' => (
      is      => 'rw',
      isa     => 'Bool',
      default => 0,
  );
  requires 'save_state', 'load_state';
  sub stop { ... }
  sub start { ... }
  package Restartable::ButUnreliable;
  use Moose::Role;
  with 'Restartable' => { alias  => { stop  => '_stop',
                                      start => '_start' } };
  sub stop {
      my $self = shift;
      $self->explode() if rand(1) > .5;
      $self->_stop();
  }
  sub start {
      my $self = shift;
      $self->explode() if rand(1) > .5;
      $self->_start();
  }
  package Restartable::ButBroken;
  use Moose::Role;
  with 'Restartable' => { excludes => [ 'stop', 'start' ] };
  sub stop {
      my $self = shift;
      $self->explode();
  }
  sub start {
      my $self = shift;
      $self->explode();
  }
```

## DESCRIPTION

Sometimes when you include a role in a class, you may want to leave out some of its methods. In this example, we have a role `"Restartable"` which provides an `"is_paused"` attribute, and two methods, `"stop"` and `"start"`. The implementation of those two methods is irrelevant.

Then we have two more roles which also implement the same interface, each putting their own spin on the `"stop"` and `"start"` method.

In the `"Restartable::ButUnreliable"` role, we want to provide a new implementation of `"stop"` and `"start"`, but still have access to the original implementation. To do this, we alias the methods from `"Restartable"` to private methods, and provide wrappers around the originals (1).

In the `"Restartable::ButBroken"` role, we want to provide an entirely new behavior for `"stop"` and `"start"`, so we exclude them when composing the `"Restartable"` role into `"Restartable::ButBroken"`.

It's worth noting that the `"excludes"` parameter also accepts a single string as an argument if you just want to exclude one method.

## CONCLUSION

Method exclusion and renaming can come in handy, especially when building roles out of other roles. In this example, all of our roles implement the `"Restartable"` role. Each role provides same API, but each has a different implementation under the hood.

You can also use the method aliasing and excluding features when composing a role into a class.

## FOOTNOTES

- (1)

  The mention of wrapper should tell you that we could do the same thing using method modifiers, but for the sake of this example, we don't.

## AUTHOR

Dave Rolsky \<autarch@urth.org\>

## COPYRIGHT AND LICENSE

Copyright 2006-2008 by Infinity Interactive, Inc.

\<http://www.iinteractive.com\>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
