3
對象不可見鑒於以下幾點:修飾符下從服務
- 武2.001001;
- 麪包::板0.34;
- 一個
Bread::Board
容器 - 基類
Foo
與屬性str
和around
改性劑str
- 子類
Bar
延伸Foo
問題:
- [GOOD]從普通實例化對象時
Bar
,Foo::str
修改器被執行; - [BAD]當從容器服務實例化對象爲
Bar
,Foo::str
修飾符時不執行; - [WORSE] [BAD]發生後,從普通
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_
字符串。
爲什麼?
問題正在調查https://github.com/stevan/BreadBoard/issues/61 – Yanick