in reply to Local for lexicals

Maybe something like this?

our $x = 1; my $f = sub { $x }; { local $x; $x = 2; print $f->(), "\n"; } print $f->(), "\n";

It doesn't give you a way to make my-declared variables local, though.

Replies are listed 'Best First'.
Re^2: Local for lexicals
by JadeNB (Chaplain) on Aug 10, 2009 at 15:44 UTC
    (I thought I posted this already, but it doesn't seem to have appeared. Let's see if I get two dups in a single thread!)

    Thanks for the suggestion, but, unfortunately, it's only the bit in the inner scope that I control—given an existing coderef $f that closes over an existing lexical $x, I want to write some code that changes $x, but only temporarily, in such a way that $f ‘sees’ the change.