On Fri, 2004-09-24 at 10:03, KJ wrote:

> So, my question is, why would one need lexical pads anyway (why are they 
> there)?

They are there so that variables can be found by name in a lexically
scoped way. One example, in Perl 5, of this need is:

        my $foo = 1;
        return sub { $foo ++ };

Here, you keep this pad around for use by the anon sub (and anyone else
who still has access to that lexical scope) to find and modify the same
$foo every time. In this case it doesn't look like a "by-name" lookup,
and once optimized, it probably won't be, but remember that you are
allowed to say:

        perl -le 'sub x {my $foo = 1; return sub { ${"foo"}++ } }$x=x();print $x->(), 
$x->(), $x->()'

Which prints "012" because of the ability to find "foo" by name.

Of course, you can emulate this behavior, but in doing so, you're going
to have to invent the pad :)

Someone else suggested that you need this for string eval, but you don't
really. You need it for by-name lookups, which string evals just happen
to also need. If you can't do by-name lookups, then string eval doesn't
need pads (and thus won't be able to access locals).

-- 
â 781-324-3772
â [EMAIL PROTECTED]
â http://www.ajs.com/~ajs

Reply via email to