|
NAMERole::Hooks - role callbacks SYNOPSIS package Local::Role {
use Moo::Role;
use Role::Hooks;
Role::Hooks->after_apply(__PACKAGE__, sub {
my ($role, $target) = @_;
print "$role has been applied to $target.\n";
});
}
package Local::Class {
use Moo;
with "Local::Role"; # prints above message
}
DESCRIPTIONThis module allows a role to run a callback when it is applied to a class or to another role. CompatibilityIt should work with Role::Tiny, Moo::Role, Moose::Role, Mouse::Role, Role::Basic, and Mite. Not all class builders work well with all role builders (for example, a Moose class consuming a Mouse role). But when they do work together, Role::Hooks should be able to run the callbacks. (The only combination I've tested is Moo with Moose though.) Some other role implementations (such as Moos::Role, exact::role, and OX::Role) are just wrappers around one of the supported role builders, so should mostly work. With Role::Basic, the "after_apply" hook is called a little earlier than would be ideal; after the role has been fully loaded and its methods have been copied into the target package, but before handling "requires", and before patching the "DOES" method in the target package. If you are using Role::Basic, consider switching to Role::Tiny. With Mite, the "before_apply" hook is called fairly late; after the role is fully loaded and attributes and methods have been copied into the target package, after "DOES" has been patched, but before method modifiers from the role have been applied to the target package. Apart from Role::Tiny/Moo::Role, a hashref of additional arguments (things like "-excludes" and "-alias") can be passed when consuming a role. Although I discourage people from using these in general, if you need access to these arguments in the callback, you can check %Role::Hooks::ARGS. Roles generated via Package::Variant should work; see t/20packagevariant.t for a demonstration. Methods
ENVIRONMENTThe environment variable "PERL_ROLE_HOOKS_DEBUG" may be set to true to enable debugging messages. BUGSPlease report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Role-Hooks>. SEE ALSORole::Tiny, Moose::Role. AUTHORToby Inkster <tobyink@cpan.org>. COPYRIGHT AND LICENCEThis software is copyright (c) 2020-2022 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 WARRANTIESTHIS 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.
|