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

I think ikegami is right after all.

Consider:

use strict; { my $a = \$a; print "a=$a\n"; sub f { my $var = shift; print $var, "=", eval "\$$var", "\n"; }; } &f("a");

$a references itself, so it will never be garbage-collected, yet it is not accessible in the eval.

Replies are listed 'Best First'.
Re^4: Accessing lexicals in other scopes dynamically by name
by LanX (Saint) on Jul 30, 2010 at 20:52 UTC
    Indeed it has nothing (or little) to do with garbage collection.

    IMHO it's the dynamic scope (i.e. the scope local has), eval can access the surrounding lexpads of f's closure only as long as there is a call chain connecting them, because the "capture" of $a is missing!

    use strict; { my $a = \$a; print "a=$a\n"; sub f { my $var = shift; print $var, "=", eval "\$$var", "\n"; }; i(); # works } #&f("a"); sub g { f("a") } sub h { g() } sub i { h() }

    Cheers Rolf