MooX::CaptainHook - hooks for MooX modules
{ 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" }
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.
MooX::CaptainHook
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.
on_application
$_[0][0]
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)].)
$_[0][1]
$_[0] = [qw(Y X)]
$_[0] = [qw(Z Y)]
Altering the $_[0] arrayref will alter what is passed to subsequent callbacks, so is not recommended.
$_[0]
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.
on_inflation
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].
$_
You can pass a package name as an optional second parameter:
use MooX::CaptainHook; MooX::CaptainHook::on_application { my ($applied_to, $role) = @_; ...; } 'Your::Role';
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=MooX-ClassAttribute.
Moo, MooX::ClassAttribute.
Toby Inkster <tobyink@cpan.org>.
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.
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.
To install MooX::ClassAttribute, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooX::ClassAttribute
CPAN shell
perl -MCPAN -e shell install MooX::ClassAttribute
For more information on module installation, please visit the detailed CPAN module installation guide.