Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Being able to run code more than once using the same variable isn't enough to be "a closure".

a closure is a data structure that holds an expression and an environment of variable bindings in which that expression is to be evaluated.

There might be a data structure involved in the not-first iterations of the

{ my $foo = 99; CODE: print ++$foo, "\n"; } goto CODE;

example. But such a data structure is not evident and you can't call the example "a closure" (based on that definition and also IMHO). You could postulate that the example hints at a closure being use behind the scenes.

But I suspect that you might be hard pressed to find one data structure that contains the 'print' "expression" and the "environment" containing the second(?) instance of $foo or you'd have to define your one "data structure" as something that contains way more stuff than that.

The "named closure" examples are an edge case for me. I don't particularly mind people calling them a closure but I'm more likely to restrict my use of that word because I think a narrower definition of that word is usually more useful.

I call the "named closure" case "static (or 'state') variables that happen to be implemented using a closure". And it turns out that the closure in that case is stored in the symbol table and when that storing happens is actually important.

for( 2,3,5,7 ) { my $x= my $y= $_; sub pow { return $y *= $x; } } print pow(), $/ for 1..3; __END__ 4 8 16

Unfortunately, Perl isn't (currently) smart enough to warn about that case, but it will warn about this equivalent:

sub wow { my $x= my $y= shift @_; sub pow { return $y *= $x; } } wow($_) for 2, 3, 5; print pow(), $/ for 1..3; __END__ Variable "$y" will not stay shared at - line 4. Variable "$x" will not stay shared at - line 4. 4 8 16

A closure is generated when that code is compiled. And that closure is stored in *{$main::{pow}}{CODE}. When new instances of $x and $y are created, no new closures are created.

So I usually only talk about closures when I'm talking about the reference to the code (the reference that also contains references to some closed-over variable instances). If the reference isn't evident, then "closure" or not usually is an implementation detail.

- tye        

In reply to Re^6: Closure on Closures ("a") by tye
in thread Closure on Closures by broquaint

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2018-02-24 22:12 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (311 votes). Check out past polls.