Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Lexicals, Pads, and Closures

by chromatic (Archbishop)
on Oct 11, 2003 at 22:21 UTC ( [id://298556]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Re: Re: Re^3: Perl to Ruby translator?
in thread Perl to Ruby translator?

That's exactly what makes it a closure.

Lexical variables live in their own symbol tables (that's not what they're called in Perl 5, but it's the concept: just a mapping of a name to the data structure that represents the appropriate variable container). They nest — that's why you can write code like this:

my $foo = 'outside'; { my $foo = 'inside'; print "Inside: '$foo'\n"; } print "Ouside: '$foo'\n";

If you dump the optree with something like B::Concise, you'll see enter, leave, and pad operations. The enter and leave ops govern which symbol table (scratch pad or lexical pad, in Perl 5 terms) is active. Think of it like a stack and you'll get it. The pad operations look up a symbol in the scratch pad.

Subroutines (anonymous and named) are represented by data structures called CVs (basically, Code Values). Every CV is attached to a lexical symbol table. That's just a place to store the lexical variables declared and used within that subroutine.

To handle the nesting issue, there has to be a way to look outward from inner scopes. If there's no $foo in the tightest, most innermost scope, look outward, one scope at a time, to find the nearest appropriate $foo. (This is complicated a little bit by global variables, which are handled with a different symbol table implementation in Perl 5. There also appears to be variable analysis because of the pad ops, not glob ops. Insert handwaving here; I really don't want to read that code right now.)

The code example you're asking about is a closure so that nested variable scopes will work. Or, put the way it actually happened, because nested variable scopes had to work, closures were possible.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-03-29 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found