The problem is that Local::Role is applied to Local::Class via with, which internally uses apply_roles_to_package. This means that apply_roles_to_package() is executed before it is wrapped by the call to on_application() in Local::Role so the hook never gets fired.
As shown in the last example above, loading Local::Role first as a module applies the wrapper, so that when Local::Class is loaded and applies Local::Role, the wrapper comes into play.
The code in the MooX::CaptainHook
synopsis only works if it's in a single file; Splitting the code into separate modules (see attached tar ball, test.tar.gz) will not.
To wit:
% perl -Ilib all-in-one.pl Local::Role applied to Local::Class
Works!
% perl -Ilib -MLocal::Class -e 1
Doesn't work.
% perl -Ilib -MLocal::Role -MLocal::Class -e 1 Local::Role applied to Local::Class
Works!
The problem is that Local::Role is applied to Local::Class via
with
, which internally uses apply_roles_to_package. This means that apply_roles_to_package() is executed before it is wrapped by the call to on_application() in Local::Role so the hook never gets fired.As shown in the last example above, loading Local::Role first as a module applies the wrapper, so that when Local::Class is loaded and applies Local::Role, the wrapper comes into play.