in reply to Re^6: Accessing lexicals in other scopes dynamically by name
in thread Accessing lexicals in other scopes dynamically by name

So in this approach there is not a "capturing of all lexicals" but simply remembering one data-structure regardless of how many variables were defined it.

You forgot the bit about placing them in the data structure to being with.

  • Comment on Re^7: Accessing lexicals in other scopes dynamically by name

Replies are listed 'Best First'.
Re^8: Accessing lexicals in other scopes dynamically by name
by morgon (Priest) on Jul 30, 2010 at 22:37 UTC
    You forgot the bit about placing them in the data structure to being with.
    I made it not clear I think.

    This structure is initialized when a new lexial scope is encountered (pointing back to the enclosing environment) and new variable bindings are added whenever a new lexial variable is defined.

    It exists whether or nor a closure is created.

    All closures within this context then simply remember a reference to this structure and use it to look up variables.

    But Perl evidently does it differently it does not really matter..

      It exists whether or nor a closure is created.

      You're talking about making references to variables all over the place to replace a few when an anon sub is created. That's a very big negative.

        I am sorry, I think I expained what I meant rather poorly and we are probably not talking about the same things.

        I was just refering to an implementation of Scheme in Scheme in which for every lexical scope a data-structure (called an "environment") was created which basically is a symbol-table. This symbol-tables also point back to the symbol-table of the enclosing lexical scope and this link is then followed when one has to look up a symbol which is not found in the current scope.

        This symbol-table is always needed by the interpreter to keep the variable-bindings - regardless of whether there are closures or not - and can be used to implement closures.

        In such a scenarion a closure would simply refer to an already existing data-structure containing all variable-bindings - so the number of variables is irrelevant for the cost of creating a closure and >>all<< variable bindings are available to the closure.

        Naively I had assumed that this was a standard technique and Perl would do the same thing, which is why I was surprised when you demonstrated that there are lexial variables that are defined when the closure is created, yet are not accessible to it because Perl could not "see" that the closure would reference them indirectly.