鉴于以下情况:
- 哞2.001001;
- 面包::板 0.34;
- 一个
Bread::Board
容器 Foo
具有属性str
和around
修饰符的基类forstr
- 子类
Bar
扩展Foo
问题:
- [GOOD] 从 plain 实例化对象时
Bar
,Foo::str
执行修饰符; - [BAD] 从容器服务为 实例化对象时
Bar
,Foo::str
不执行修饰符; - 在 [BAD] 发生后 [WORSE],从普通实例化
Bar
也不再有效;
代码示例:
#!/usr/qlc/apps/common/perl_5.18.2/bin/perl -w
package Foo;
use Moo;
has str => ( is => 'rw', default => '' );
sub BUILD {
my ($self, $args) = @_;
$self->str($self->str);
}
around str => sub {
my ($orig, $self, $val) = @_;
return $self->$orig unless defined $val;
$self->$orig('prefix_'.$val);
};
# end of Foo
package Bar;
use Moo;
extends 'Foo';
# end of Bar
package main;
use 5.010;
use strictures 2;
use Bread::Board;
my $c = container 'MyApp' => as {
service 'foo' => ( class => 'Foo', parameters => { str => { optional => 1 } } );
service 'bar' => ( class => 'Bar', parameters => { str => { optional => 1 } } );
};
my $foo_plain = Foo->new({ str => 'foo_plain' });
say "foo_plain = ".$foo_plain->str;
my $foo_bb = $c->resolve( service => 'foo', parameters => { str => 'foo_bb' } );
say "foo_bb = ".$foo_bb->str;
$foo_bb->str('foo_bb_setter');
say "foo_bb_setter = ".$foo_bb->str;
my $foo_plain_after_bb = Foo->new({ str => 'foo_plain_after_bb' });
say "foo_plain_after_bb = ".$foo_plain_after_bb->str;
my $bar_plain = Bar->new({ str => 'bar_plain' });
say "bar_plain = ".$bar_plain->str;
my $bar_bb = $c->resolve( service => 'bar', parameters => { str => 'bar_bb' } );
say "bar_bb = ".$bar_bb->str;
$bar_bb->str('bar_bb_setter');
say "bar_bb_setter = ".$bar_bb->str;
my $bar_plain_after_bb = Bar->new({ str => 'bar_plain_after_bb' });
say "bar_plain_after_bb = ".$bar_plain_after_bb->str;
die;
输出:
foo_plain = prefix_foo_plain
foo_bb = prefix_foo_bb
foo_bb_setter = prefix_foo_bb_setter
foo_plain_after_bb = prefix_foo_plain_after_bb
bar_plain = prefix_bar_plain
bar_bb = bar_bb
bar_bb_setter = bar_bb_setter
bar_plain_after_bb = bar_plain_after_bb
请注意输出bar_bb
,bar_bb_setter
并且bar_plain_after_bb
不包含prefix_
字符串。
为什么?