Pass variables around modifier around method

Is it possible to pass variables between multiple calls in around MethodModier ? example (this does not work, but hopefully conveys what I want)

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'
       ;
};

In the end, I wanted these "circles" to be placed in plug-in lines, where I do not know which ones were loaded in advance, but the final output will be neatly formatted.

Perhaps I think about it completely wrong, so welcome other suggestions.

+1
source share
2 answers

Use the instance variable:

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

(See the answers to the questions for the actual answer. I am simply repeating this here, so I can accept the answer, thanks to:

jmz)

0

Source: https://habr.com/ru/post/1759762/


All Articles