Why does the Moose role application with method modifiers not work in my code?

I have a Role and several classes that mix the role. The Role class loads all implementation classes, so everything that Blah imports can use them without typing many "use" lines.

package Blah;
use Moose::Role;

use Blah::A;
use Blah::B;
(etc...)

requires '...';
requires 'foo';
around 'foo' => sub { ... }

Typical Blah subclass:

package Blah::A;
use Moose;
with 'Blah';

sub foo { ... }

__PACKAGE__->meta->make_immutable;

Since each method of the "foo" subclass starts with the same bits of code, this role also implements them using the method modifier.

The problem is that Moose does not apply the method modifier to any of the Blah :: * classes. This happens even if I remove the make_immutable call for classes. I thought the role application was completely executed at runtime, and therefore, although the Blah :: * classes are loaded before Blah, should the modifier still apply?

. Blah , , - , , ? .

+3
2

- use ing Blah:: A , Blah:: A?

use , ( () ). , , use, , Includes.

, - - , with. use , , with ( , ). , (, . Apply_all_roles Moose::Util), , .

+4

, .

use require, inferred Package import() BEGIN {}.

:: MOP (CMOP). , , CMOP , - add_method. irc.perl.org/#moose, . , , , , .

package Class;
use Moose;
use Carp qw(carp);

### You have to add the method onto the class.
Class->meta->add_method( 'carp' => \&carp );

around 'carp' => sub { warn "this won't trigger" };

package main;

my $c = Class->new;
$c->carp('foo');

, use ing Moose, , with. .

-1

Source: https://habr.com/ru/post/1732909/


All Articles