in reply to Re: Perl scoping not logical...?
in thread Perl scoping not logical...?
I'm not sure that's entirely accurate, but I'm not certain I understand exactly what you mean.
All Perl subroutines -- named or not -- get compiled during compile time into optrees accessed through an internal data structures called a CV. Named subroutines get stored in global symbol tables: hashes where the keys are the names of the subroutines and the values are CVs. Anonymous subroutines are stored elsewise (and I'm not certain enough of how to explain it well).
CVs have arrays of lexpads attached. Lexpads store lexicals. There are multiple lexpads because any particular point in a program can have called through the same subroutine multiple times, and because lexical scopes nest.
When you create a new closure at runtime, Perl doesn't recompile the code and create a new CV. It reuses the anonymous subroutine's CV, and attaches the current innermost lexpad. (In detail, there's probably a cloning operation here, but I don't want to get too much into the details.)
This should be enough to explain why lexical variables won't stay shared.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Perl scoping not logical...?
by John M. Dlugosz (Monsignor) on May 02, 2008 at 01:14 UTC |