NAME

MooX::CaptainHook - hooks for MooX modules

SYNOPSIS

{
   package Local::Role;
   use Moo::Role;
   use MooX::CaptainHook qw(on_application);
   
   on_application {
      print "Local::Role applied to $_\n";
   };
}

{
   package Local::Class;
   use Moo;
   with 'Local::Role'; # "Local::Role applied to Local::Class"
}

DESCRIPTION

Although developed to support MooX::ClassAttribute, MooX::CaptainHook provides a feature which may be of use to other people writing Moo roles and MooX modules.

This module allows you to run callback code when various events happen to Moo classes and roles. Callback code for a role will also be copied as hooks for any packages that consume that role.

on_application { BLOCK }

The on_application hook allows you to run a callback when your role is applied to a class or other role. Within the callback $_[0][0] is set to the name of the package that the role is being applied to.

Also $_[0][1] is set to the name of the role being applied, which may not be the same as the role where the hook was initially defined. (For example, when role X establishes a hook; role X is consumed by role Y; and role Y is consumed by class Z. Then the callback code will run twice, once with $_[0] = [qw(Y X)] and once with $_[0] = [qw(Z Y)].)

Altering the $_[0] arrayref will alter what is passed to subsequent callbacks, so is not recommended.

on_inflation { BLOCK }

The on_inflation hook runs if your class or role is "inflated" to a full Moose class or role. $_[0][0] is the associated metaclass.

Setting $_[0][0] to a new meta object should "work" (whatever that means).

is_role($package)

Returns a boolean indicating whether the package is a role.

Within callback code blocks, $_ is also available as a convenient alias to $_[0][0].

Installing Hooks for Other Packages

You can pass a package name as an optional second parameter:

use MooX::CaptainHook;

MooX::CaptainHook::on_application {
   my ($applied_to, $role) = @_;
   ...;
} 'Your::Role';

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=MooX-ClassAttribute.

SEE ALSO

Moo, MooX::ClassAttribute.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2013 by Toby Inkster.

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

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.