A closure is an anonymous subroutine that's been created by another subroutine.
Not quite. That's a bad meme that perists, despite our efforts to the contrary.

A closure is subroutine that has captured its lexical environment which has now gone out of scope.

This is orthogonal to the concept of an anonymous subroutine, which may or may not need to be "closed". In other words, the properties of "having a name" and "being a closure" are uncorrelated.

Here's a named subroutine that's a closure:

BEGIN { my $current = 0; sub gimme_next { ++$current } }
This named subroutine gimme_next is a closure. And here's an example of an anonymous subroutine which is not a closure:
my $foo = sub { 2 * shift };
So, please don't confuse anonymous subroutine with closure: they're really different things.

-- Randal L. Schwartz, Perl hacker


In reply to •closures are NOT anonymous subroutines necessarily by merlyn
in thread subs as args by Anonymous Monk

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.