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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.