在 around 方法修饰符周围传递变量
是否可以在多次调用之间传递变量到 around
MethodModier?示例(这不起作用,但希望传达我想要做的事情)
sub mysub { ... };
around 'mysub' => sub {
my $orig = shift;
my $self = shift;
my $value = get_value;
$self->orig(@_);
};
around 'mysub' => sub {
my $orig = shift;
my $self = shift;
my $value = shift;
my $output
= "sometext $value"
. $self->orig(@_);
. 'someothertext $value'
;
};
我最终希望将这些“周围”放置在可插入特征中,我不会真正知道预先加载了哪些特征,但最终输出将是格式整齐。
我的想法可能完全错误,所以欢迎其他建议。
Is it possible to pass variables between multiple calls to the around
MethodModier? example (that doesn't work but hopefully conveys what I want to do)
sub mysub { ... };
around 'mysub' => sub {
my $orig = shift;
my $self = shift;
my $value = get_value;
$self->orig(@_);
};
around 'mysub' => sub {
my $orig = shift;
my $self = shift;
my $value = shift;
my $output
= "sometext $value"
. $self->orig(@_);
. 'someothertext $value'
;
};
I'd eventually like to have these 'arounds' placed in pluggable traits, where I won't really know which ones are loaded beforehand but the final output will be neatly formatted.
It's possible that I'm thinking about this completely wrong, so other suggestions welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你试图做的事情没有逻辑。
https://metacpan .org/pod/Moose::Manual::MethodModifiers#BEFORE-AFTER-AND-AROUND
What you are trying to do don't have logic.
https://metacpan.org/pod/Moose::Manual::MethodModifiers#BEFORE-AFTER-AND-AROUND
使用实例变量:
(请参阅问题评论以获取实际答案。我只是在这里重申,因此我可以接受答案,感谢:
)
Use an instance variable:
(See question commments for an actual answer. I'm just reiterating it here, so I can accept an answer, thanks to:
)