Problems? Is your data what you think it is? | |
PerlMonks |
Re^8: Unclear about 'our'by LanX (Saint) |
on Dec 29, 2022 at 11:04 UTC ( [id://11149183]=note: print w/replies, xml ) | Need Help?? |
> 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.
Cheers Rolf
updates°) lexical vars in this case, package vars are just globally available...
In Section
Seekers of Perl Wisdom
|
|