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

You never captured $x. It was simply still in scope. You basically did
my $x="global"; my $test=sub { my $x="pre"; alert($x); # pre my ($name,$val)=@_; eval('$'.$name."='".$val."'"); alert($x); # post }
which we've already established should work. Call the sub from outside the scope in which $x resides and you'll see it fail.

Replies are listed 'Best First'.
Re^4: Accessing lexicals in other scopes dynamically by name
by LanX (Saint) on Jul 30, 2010 at 17:22 UTC
    >Call the sub from outside the scope in which $x resides and you'll see it fail.

    please define your understanding of "capture" and "scope" (dynamic or static).

    Cheers Rolf

      Capturing preserves a variable beyond the end of its normal scope.
      sub make_closure { my ($x) = @_; return sub { $x }; } my $c1 = make_closure("a"); my $c2 = make_closure("b"); say $c1->(); # a say $c2->(); # b

      The $x from the first call to make_closure and the $x from the second call to make_closure are kept alive beyond the end of their normal scope (the end of that call to make_closure).