我有一些使用MooseX::Traits的代码,可以动态加载角色。由于角色相互依赖,因此其中一个角色的设置要求似乎不起作用。是否有正确的方法来实现下面显示的模式,此代码将以died: Moose::Exception::RequiredMethodsNotImplementedByClass ('Constructor::Plugin' requires the method 'frobnicate' to be implemented by 'MooseX::Traits::__ANON__::SERIAL::1'
#!/usr/bin/perl
package Constructor;
use Moose;
with 'MooseX::Traits';
use namespace::autoclean;
has '+_trait_namespace' => ( default => __PACKAGE__);
sub load {
my ($class, $plugin) = @_;
my $obj_with_plugin = $class->new_with_traits(traits => ["$plugin"]);
return $obj_with_plugin;
}
__PACKAGE__->meta->make_immutable;
1;
package Constructor::Plugin;
use Moose::Role;
requires 'frobnicate';
with 'Constructor::Plugin::Component';
1;
package Constructor::Plugin::Component;
use Moose::Role;
sub frobnicate { "zark!\n" }
1;
package main;
use Test::More;
use Test::Exception;
my $thing;
lives_ok {$thing = Constructor->load('Plugin')} "instantiated ok";
ok $thing->frobnicate, "frobnicates ok";
done_testing;
答案 0 :(得分:1)
如何通过MooseX :: Traits加载Constructor :: Plugin :: Component?
#!/usr/bin/perl
package Constructor;
use Moose;
with 'MooseX::Traits';
use namespace::autoclean;
has '+_trait_namespace' => ( default => __PACKAGE__);
sub load {
my ($class, @plugin) = @_;
my $obj_with_plugin = $class->new_with_traits(traits => [@plugin]);
return $obj_with_plugin;
}
__PACKAGE__->meta->make_immutable;
1;
package Constructor::Plugin;
use Moose::Role;
requires 'frobnicate';
1;
package Constructor::Plugin::Component;
use Moose::Role;
sub frobnicate { "zark!\n" }
1;
package main;
use Test::More;
use Test::Exception;
my $thing;
lives_ok {$thing = Constructor->load('Plugin', 'Plugin::Component')} "instantiated ok";
ok $thing->frobnicate, "frobnicates ok";
done_testing;