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

ikegami, you are right.

Does anyone have any idea why Perl does it like that?

I mean to implement it like this must be more difficult than simply to keep a pointer to the whole enviroment for every closure.

What is the motivation?

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

Replies are listed 'Best First'.
Re^5: Accessing lexicals in other scopes dynamically by name
by ikegami (Patriarch) on Jul 30, 2010 at 20:13 UTC
    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.
      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.