1

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.

xenoterracide
  • 16,274
  • 24
  • 118
  • 243
  • How about using instance variables? – jmz Aug 15 '10 at 11:02
  • @jmz instance variables? – xenoterracide Aug 15 '10 at 11:05
  • Like `$self->{value} = get_value;` .... `my $value = $self->{value};` – jmz Aug 15 '10 at 11:35
  • @jmz that works... why didn't I think of that... thanks. – xenoterracide Aug 15 '10 at 11:53
  • @jmz although I find it odd that the order of the around's is the exact opposite of what I'd think it would be, in order for it to work correctly. – xenoterracide Aug 15 '10 at 12:18
  • 1
    @xenoterracide: MethodModifier documentation is quite clear that the order is around2..around1...orig. – jmz Aug 15 '10 at 12:45
  • @jmz yeah... I read the docs on it... anyways that's not so much a problem, though I realized I need to build on your solution a bit. I think I need to use a LIFO stack. – xenoterracide Aug 15 '10 at 13:08
  • 1
    @jmz: by "instance variables" I hope you mean attributes. If I saw `$self->{value} = ...` in a Moose module I would slap the programmer. @xenoterracide: `around` application on a method *is* a LIFO stack. – Ether Aug 15 '10 at 15:22
  • @Ether obviously but I need more than just the `around` for this. read the Catalyst Documentation? it's full of `$self->{value}` – xenoterracide Aug 15 '10 at 15:50

2 Answers2

0

What you are trying to do don't have logic.

"An around modifier receives the original method as its first argument, then the object, and finally any arguments passed to the method."

https://metacpan.org/pod/Moose::Manual::MethodModifiers#BEFORE-AFTER-AND-AROUND

szabgab
  • 6,202
  • 11
  • 50
  • 64
Mantovani
  • 500
  • 2
  • 7
  • 18
0

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:

jmz)

Community
  • 1
  • 1
xenoterracide
  • 16,274
  • 24
  • 118
  • 243