在 around 方法修饰符周围传递变量 [英] Pass variables around the around method modifier

查看:78
本文介绍了在 around 方法修饰符周围传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在多次调用之间传递变量到 around )

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.

解决方案

Use an instance variable:

$self->{value} = get_value;
...
my $value = $self->{value};

(See question commments for an actual answer. I'm just reiterating it here, so I can accept an answer, thanks to:

)

这篇关于在 around 方法修饰符周围传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆