> Personally, I tend to think of closures as something you can make multiple instances of

That's a further concept, building on top of closures.

In what you show foo is a "generator" returning an anonymous sub, which is indeed enclosing $x ... but that's not necessary for a closure.

A "closure" is the combination of a sub and "enclosed"° vars from the outer scope.

you don't need generators for closures...

see also Closure (computer programming) : "Operationally, a closure is a record storing a function together with an environment."

But it's true that many - including me - often simply say "closure" for the "closure function"

> but if the code you showed was in a block, then I guess that can fit the definition of a closure too

an explicit block is just cosmetics, since the file-scope acts as a default block.

> since the subs are then the only thing to hold references to the variables.

That's bit confusing ... a sub will only hold reference to a variable form a surrounding scope if it's enclosed, i.e. explicitly used. You don't need a block to enforce this.

Consider this example, where the second bar() will fail because there is no explicit use of $var, just a symbolic reference.

Since it's not bound - here realized as the scratch pads of the sub increasing the ref-count - it can be destroyed.

NB: I just need the block here because all calls are in the same file.

use v5.12; use warnings; use Data::Dump qw/pp dd/; { my $var = 42; sub foo { warn $var; } sub bar { eval 'warn $var'; } foo(); bar(); } foo(); bar();

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

updates

°) lexical vars in this case, package vars are just globally available...


In reply to Re^8: Unclear about 'our' by LanX
in thread Unclear about 'our' by ibm1620

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.