Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Closure on Closures (beta)

by sauoq (Abbot)
on Jul 07, 2003 at 02:52 UTC ( [id://271858]=note: print w/replies, xml ) Need Help??


in reply to Closure on Closures

However, I believe this isn't entirely accurate as a closure in perl can be any subroutine referring to lexical variables in the surrounding lexical scopes.

I might take it a step further and say "any code." Here is an example where someone accidentally created a closure with a code assertion in a regular expression.

I also offer the following variation on your bare block example for discussion:

{ my $foo = 99; CODE: { print ++$foo, "\n" }; } goto CODE;
It isn't intuitive, but that will print "100" once and then print integers counting up from 1. Given that the storage isn't the same on subsequent entries into the bare block as it is on the first one, one might argue that it isn't a closure at all. It makes sense, however, to think of it as a closure that is created on the first entry into the bare block via the goto. Of course, that makes it impossible to initialize the data. So, it's not very useful (except maybe for obfuscations.)

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: Closure on Closures (beta)
by adrianh (Chancellor) on Jul 07, 2003 at 13:15 UTC

    I don't think this would fit in the normal CS definition of a closure. With closures you are creating a data structure that holds some code and the environment the code will be evaluated in.

    In your example you're moving to some code that's inside a scope, rather than creating some code that carries it's scope with it.

      I don't think this would fit in the normal CS definition of a closure.

      I think the "normal CS definition of a closure" is a pretty elusive beast; mythical, maybe. I've been unsuccessfully looking for one since tye challenged me to find one in his reply to this node. Most of the time, the term is defined within the context of a particular language. Usually, it is demonstrated rather than defined at all.

      Insofar as creating a definition, I disagree with tye about the importance of being able to "hide different lexicals into seperate refences to the same subroutine." (I absolutely agree that being able to do so is what really makes closures useful, however.)

      If tye had tasked me with writing a generic definition for "closure" rather than finding one, I would have responded with something similar to this:

      A closure is code that retains a binding to a free variable even after the variable has gone out of scope.
      I think that describes what a closure is without resorting to explaining what one is useful for, or how one is implemented.

      In your example you're moving to some code that's inside a scope, rather than creating some code that carries it's scope with it.

      Well, yes and no. (And this is, at least in part, why I submitted it for discussion.) I suggested that the closure is actually created when the sub is first entered via the goto. At that point, the variable has gone out of scope. Then we jump back into the code after the declaration. Really, it's very similar to broquaint's "named closure" example; only, the inner block is not a subroutine. I don't know the details of what perl is doing with the relevant scratchpads at that point, and I'd be happy for someone to explain it to me. I suspect that the variable is re-initialized to undef because perl doesn't ever expect a bare block to be used like a closure but I'm not sure why it isn't consistently re-initialized (except that we never explicitly declare it again.)

      Perhaps you disagree that a "named closure" is a closure at all and I suppose that is debatable. I think a "named closure" fits the definition that I gave above though, so I'm in agreement with broquaint when he says that he doesn't think the Camel (3rd edition) is entirely accurate in its "anonymous function" requirement.

      -sauoq
      "My two cents aren't worth a dime.";
      
        I think the "normal CS definition of a closure" is a pretty elusive beast; mythical, maybe.

        The The Free On-line Dictionary of Computing's

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

        works for me.

        A closure is code that retains a binding to a free variable even after the variable has gone out of scope.

        Close enough :-)

        I suggested that the closure is actually created when the sub is first entered via the goto.

        So is this a closure?:

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

        I don't think the above, or your original example, meet your or my definitions of a closure.

        The variable bindings are not retained - because we don't get 100, 101, etc. That binding is an essential part of what a closure is all about.

        Really, it's very similar to broquaint 's "named closure" example; only, the inner block is not a subroutine

        I don't think it is because "named" closures retain the variable bindings of their scope they were declared in.

        I do agree with you and broquaint that the Camel's definition is off. Closures and naming are separate concepts. It seems to be one of those things that Perl defines differently from the rest of the world :-) Why should a closure suddenly become something else if its assigned to a symbol table entry.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://271858]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found