| 1 |
NAME |
| 2 |
Role::Hooks - role callbacks |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
package Local::Role { |
| 6 |
use Moo::Role; |
| 7 |
use Role::Hooks; |
| 8 |
|
| 9 |
Role::Hooks->after_apply(__PACKAGE__, sub { |
| 10 |
my ($role, $target) = @_; |
| 11 |
print "$role has been applied to $target.\n"; |
| 12 |
}); |
| 13 |
} |
| 14 |
|
| 15 |
package Local::Class { |
| 16 |
use Moo; |
| 17 |
with "Local::Role"; # prints above message |
| 18 |
} |
| 19 |
|
| 20 |
DESCRIPTION |
| 21 |
This module allows a role to run a callback when it is applied to a class |
| 22 |
or to another role. |
| 23 |
|
| 24 |
Compatibility |
| 25 |
It should work with Role::Tiny, Moo::Role, Moose::Role, Mouse::Role, |
| 26 |
Role::Basic, and Mite. Not all class builders work well with all role |
| 27 |
builders (for example, a Moose class consuming a Mouse role). But when |
| 28 |
they do work together, Role::Hooks should be able to run the callbacks. |
| 29 |
(The only combination I've tested is Moo with Moose though.) |
| 30 |
|
| 31 |
Some other role implementations (such as Moos::Role, exact::role, and |
| 32 |
OX::Role) are just wrappers around one of the supported role builders, so |
| 33 |
should mostly work. |
| 34 |
|
| 35 |
With Role::Basic, the `after_apply` hook is called a little earlier than |
| 36 |
would be ideal; after the role has been fully loaded and its methods have |
| 37 |
been copied into the target package, but before handling `requires`, and |
| 38 |
before patching the `DOES` method in the target package. If you are using |
| 39 |
Role::Basic, consider switching to Role::Tiny. |
| 40 |
|
| 41 |
With Mite, the `before_apply` hook is called fairly late; after the role |
| 42 |
is fully loaded and attributes and methods have been copied into the |
| 43 |
target package, after `DOES` has been patched, but before method modifiers |
| 44 |
from the role have been applied to the target package. |
| 45 |
|
| 46 |
Apart from Role::Tiny/Moo::Role, a hashref of additional arguments (things |
| 47 |
like "-excludes" and "-alias") can be passed when consuming a role. |
| 48 |
Although I discourage people from using these in general, if you need |
| 49 |
access to these arguments in the callback, you can check |
| 50 |
%Role::Hooks::ARGS. |
| 51 |
|
| 52 |
Roles generated via Package::Variant should work; see t/20packagevariant.t |
| 53 |
for a demonstration. |
| 54 |
|
| 55 |
Methods |
| 56 |
`before_apply` |
| 57 |
Role::Hooks->before_apply($rolename, $callback); |
| 58 |
|
| 59 |
Sets up a callback for a role that will be called before the role is |
| 60 |
applied to a target package. The callback will be passed two |
| 61 |
parameters: the role being applied and the target package. |
| 62 |
|
| 63 |
The role being applied may not be the same role as the role the |
| 64 |
callback was defined in! |
| 65 |
|
| 66 |
package Local::Role1 { |
| 67 |
use Moo::Role; |
| 68 |
use Role::Hooks; |
| 69 |
Role::Hooks->before_apply(__PACKAGE__, sub { |
| 70 |
my ($role, $target) = @_; |
| 71 |
print "$role has been applied to $target.\n"; |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
package Local::Role2 { |
| 76 |
use Moo::Role; |
| 77 |
with "Local::Role1"; |
| 78 |
} |
| 79 |
|
| 80 |
package Local::Class1 { |
| 81 |
use Moo::Role; |
| 82 |
with "Local::Role2"; |
| 83 |
} |
| 84 |
|
| 85 |
This will print: |
| 86 |
|
| 87 |
Local::Role1 has been applied to Local::Role2. |
| 88 |
Local::Role2 has been applied to Local::Class1. |
| 89 |
|
| 90 |
If you only care about direct applications of roles (i.e. the first |
| 91 |
one): |
| 92 |
|
| 93 |
Role::Hooks->before_apply(__PACKAGE__, sub { |
| 94 |
my ($role, $target) = @_; |
| 95 |
return if $role ne __PACKAGE__; |
| 96 |
print "$role has been applied to $target.\n"; |
| 97 |
}); |
| 98 |
|
| 99 |
If you only care about roles being applied to classes (i.e. the second |
| 100 |
one): |
| 101 |
|
| 102 |
Role::Hooks->before_apply(__PACKAGE__, sub { |
| 103 |
my ($role, $target) = @_; |
| 104 |
return if Role::Hooks->is_role($target); |
| 105 |
print "$role has been applied to $target.\n"; |
| 106 |
}); |
| 107 |
|
| 108 |
`after_apply` |
| 109 |
Role::Hooks->after_apply($rolename, $callback); |
| 110 |
|
| 111 |
The same as `before_apply`, but called later in the role application |
| 112 |
process. |
| 113 |
|
| 114 |
Note that when the callback is called, even though it's after the role |
| 115 |
has been applied to the target, it doesn't mean the target has |
| 116 |
finished being built. For example, there might be `has` statements |
| 117 |
after the `with` statement, and those will not have been evaluated |
| 118 |
yet. |
| 119 |
|
| 120 |
If you want to throw an error when someone applies your role to an |
| 121 |
inappropriate target, it is probably better to do that in |
| 122 |
`before_apply` if you can. |
| 123 |
|
| 124 |
`after_inflate` |
| 125 |
Role::Hooks->after_inflate($pkg_name, $callback); |
| 126 |
|
| 127 |
Even though this is part of Role::Hooks, it works on classes too. But |
| 128 |
it only works on classes and roles built using Moo. This runs your |
| 129 |
callback if your Moo class or role gets "inflated" to a Moose class or |
| 130 |
role. |
| 131 |
|
| 132 |
If you set up a callback for a role, then the callback will also get |
| 133 |
called if any packages that role was applied to get inflated. |
| 134 |
|
| 135 |
`is_role` |
| 136 |
Will return true if the given package seems to be a role, false |
| 137 |
otherwise. |
| 138 |
|
| 139 |
(In fact, returns a string representing which role builder the role |
| 140 |
seems to be using -- "Role::Tiny", "Moose::Role", "Mouse::Role", |
| 141 |
"Role::Basic", or "Mite::Role"; roles built using Moo::Role are |
| 142 |
detected as "Role::Tiny".) |
| 143 |
|
| 144 |
ENVIRONMENT |
| 145 |
The environment variable `PERL_ROLE_HOOKS_DEBUG` may be set to true to |
| 146 |
enable debugging messages. |
| 147 |
|
| 148 |
BUGS |
| 149 |
Please report any bugs to |
| 150 |
<http://rt.cpan.org/Dist/Display.html?Queue=Role-Hooks>. |
| 151 |
|
| 152 |
SEE ALSO |
| 153 |
Role::Tiny, Moose::Role. |
| 154 |
|
| 155 |
AUTHOR |
| 156 |
Toby Inkster <[email protected]>. |
| 157 |
|
| 158 |
COPYRIGHT AND LICENCE |
| 159 |
This software is copyright (c) 2020-2022 by Toby Inkster. |
| 160 |
|
| 161 |
This is free software; you can redistribute it and/or modify it under the |
| 162 |
same terms as the Perl 5 programming language system itself. |
| 163 |
|
| 164 |
DISCLAIMER OF WARRANTIES |
| 165 |
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 166 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 167 |
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 168 |
|