Hello Monks,

I was trying to build a module Net::SNTP::Server, when I got some comments on why I am using anonymous subroutines perlsub instead of subroutines. This made me thinking is there a big difference in memory usage based on the type (anonymous, named’s, local) subroutines.

I was reading online on a similar question Why would I use Perl anonymous subroutines instead of a named one?. But they do not mention if one the three different ways to use a subroutine is more efficient in memory from the others.

Sample of pseudo code on anonymous subroutines:

sub module_method { ... my anonymous_inner_function = sub { ... } ... $anonymous_inner_function->(@_); ... }

Named subroutine:

sub module_method { ... sub inner_function { ... } ... inner_function(@_); ... }

It leads to nesting subs.

So I checked online and I found a useful tutorial Creating Nested Functions on how to reduce the memory consumption with local subroutines.

Local subroutine:

sub module_method { ... local *inner_function = sub { ... }; inner_function(@_); ... }

So my question is, which is more memory efficient based on memory usage? Does it make any difference in defining the subroutine anonymous or local or named when it is used internally (inside another function)? Are there more pros than cons that I am missing?

Thank you in advance everyone for their time and effort, reading and replying to my question.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Memory efficiency, anonymous vs named's vs local subroutines by thanos1983

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.