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

I think it's an optimisation. Capturing all lexicals would cause a closure to take longer to create, use more memory and prevent another optimisation that reuses lexicals from previous calls to the function.
  • Comment on Re^5: Accessing lexicals in other scopes dynamically by name

Replies are listed 'Best First'.
Re^6: Accessing lexicals in other scopes dynamically by name
by morgon (Priest) on Jul 30, 2010 at 20:36 UTC
    Hmm.

    I have never really looked at the Perl-internals, but I can vagly remember having seen an implementation of Scheme in Scheme.

    If I remember correctly there was a data-structure containing all the variable-bindings for any lexical scope and every closure would simply keep a reference to this.

    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.

    Perl evidently does this differently and I even think that the inaccessabilty of certain lexial variables of which Perl could not see that the are going to be used in a closure is a weakness.

    I will try this in Rakudo.

      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.

        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..