in reply to Nailing down closures!
and you might be tempted to write the Perl equivalent like this:(defun foo (n) (labels ((f (i) (+ i m))) f) )
But this will not work, because the inner sub "f" is really global and the closure won't behave like you want it to. The solution is to make f anonymous:sub foo { my $n = shift; sub f { my $i = shift; return $i+$n; } return \&f; }
And this is the main reason why closures in Perl are usually anonymous.sub foo { my $n = shift; my $f = sub { my $i = shift; return $i+$n; }; return $f; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Nailing down closures!
by blazar (Canon) on Dec 06, 2005 at 09:25 UTC |