and this is a costly method doing things, as the $self object would be very heavy to pass-on or call.
I'm not sure that I understand you, but I think you may be misunderstanding what your code does.
When you assign _proc = $action1, you are simply storing a reference to the code. This is no more costly in terms of either cpu or memory than storing an integer.
The only time any additional cost is incurred, is when you invoke that code via the reference at:
map($action1->($_), @arr);
And that seems to be exactly what you want (and need) to do.
The only possibility of reducing that cost, is if you find that $action1->( ... ) is being called with the same input parameter many times. In that case, you may be able to defer the cost of the call, by caching the results and only calling $action1 if the required result is not already in the cache:
my %cache; sub routine { my $s = shift; $s->{_xp}->process(); my @arr = $s->{_xp}->getvalues("packet"); map{ $cache{ $_ } ||= $action1->($_) } @arr); }
Note: //= would be better than ||= if you are using v5.10 or later.
There is a module, Memoize that will do this for you, but it only appears to work on named subs, not code references.
Update: Corion points out that I hadn't read far enough into the Memoize docs, and that it will accept a code reference and return a new code reference you should use in its place.
That said, the above will work more quickly, though it could almost certainly be implemented better.
In reply to Re: A way registering an anonymous sub in $self?
by BrowserUk
in thread A way registering an anonymous sub in $self?
by harsha.reddy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |