The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

With::Roles - Create role/class/object with composed roles

SYNOPSIS

  use With::Roles;
  # create class inheriting from My::Class, with My::Role applied
  my $class = My::Class->with::roles('My::Role');

  # create a role with My::Role, then Another::Role applied
  my $role = My::Role->with::roles('Another::Role');

  # generated role can be applied
  my $obj = My::Class->with::roles($role)->new;

  # apply role to object
  $obj->with::roles('Yet::Another::Role');

  # applies the role My::Class::Role::My::Role
  my $another_class = My::Class->with::roles('+My::Role');

DESCRIPTION

This module provides an easy to use global function that can be used on any package to create a new package with a set of roles applied.

When used on classes, generates a subclass with the given roles applied.

When used on roles, generates a new role with the base and given roles applied.

When used on objects, applies the roles to the object and returns the object. Unlike with roles and classes, this modifies the invocant.

Compatible with Moose, Moo, Mouse, and Role::Tiny roles and classes.

The generated packages will have names based on the original classes and roles to aid with debugging. The exact form of the generated names should not be relied on.

A shorthand of +RoleName can be used for roles named like MyClass::Role::RoleName. Additional roles applied will continue to base the name on the original class. The package can also provide a method ROLE_BASE to return a prefix to use other than MyClass::Role. ROLE_BASE support is experimental, and may be removed or changed in a future version.

AUTHOR

haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org>

CONTRIBUTORS

None so far.

COPYRIGHT

Copyright (c) 2019 the With::Roles "AUTHOR" and "CONTRIBUTORS" as listed above.

LICENSE

This library is free software and may be distributed under the same terms as perl itself. See https://dev.perl.org/licenses/.