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

>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

  • Comment on Re^4: Accessing lexicals in other scopes dynamically by name

Replies are listed 'Best First'.
Re^5: Accessing lexicals in other scopes dynamically by name
by ikegami (Patriarch) on Jul 30, 2010 at 18:09 UTC
    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).