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.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |