http://www.perlmonks.org?node_id=11156239


in reply to Anonymous subroutines

There are various possible reasons

In general anonymous subs ...

Without more details it's hard to tell, why you saw, what you saw.

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

°) well "available", they are only compiled once at compile time, but their time window/life span is dynamic

*) even in older Perl versions

²) e.g. as callbacks

³) like in List::Util et.al.

Update

Disclaimer: The listed points are not exclusive and have overlaps. Neither do I expect them to be exhaustive and to cover all cases.

Replies are listed 'Best First'.
Re^2: Anonymous subroutines (why and what for)
by jdporter (Paladin) on Dec 11, 2023 at 20:25 UTC

    I believe this is an excellent summary. Thanks, LanX!

    One context in which I've seen anonymous subs used a lot is where you have code chunks being defined within, and called by, a framework. Execution of the code takes the form of eval'ing the code, so it has lexical context just like a sub. If, in such a code chunk, you want to define a sub so you can call it multiple times (or any other reason), you have to do it as an anonymous sub, since you can't do it any other way (at least not without getting a "won't stay shared" warning). (But your other comment is important too.)

      Thanks!

      I'm not sure I've ever worked with such kind of a "code template system" ° but I wanted to add metaprogramming anyway. (Done)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

      °) maybe apart the monastery's codebase ;)

        ;-)