On Tue, Nov 30, 2010 at 12:37 PM, Reini Urban <rurban@x-ray.at> wrote: > Sorry, I don't understand that special theory of yours. > Why on earth should > > BEGIN{my $x = 5; *foo = sub(){$x}; $x=6} print foo' > ever print 6 again? > > We are using lexical scope, not dynamic scope. > With dynamic scoping, i.e. local $x = 5; you could justify a 6. > Closures capture variables, not values. (At least in Perl. I don't know about elsewhere.) The variable can freely be changed. Given the following body of code, { set(123); say(get()); } Observe the differences depending on how those functions are defined. BEGIN { # Works my $private; sub get() { $private } sub set($) { $private = $_[0]; } } BEGIN { # Doesn't work my $private; *get = sub() { $private }; *set = sub($) { $private = $_[0]; }; } BEGIN { # Works my $private; *get = sub { $private }; *set = sub($) { $private = $_[0]; }; } This has nothing to do with dynamic scope. Dynamic scoping would allow get() and set() to see a variable that resides their caller. - EricThread Previous | Thread Next