in reply to Nailing down closures!

To get at your question more directly, it is true that closures in Perl do not have to be anonymous, and as others have said, yes technically your example is a closure. But the canonical case for named closures is something like this in Lisp (forgive my somewhat rusty Lisp):
(defun foo (n) (labels ((f (i) (+ i m))) f) )
and you might be tempted to write the Perl equivalent like this:
sub foo { my $n = shift; sub f { my $i = shift; return $i+$n; } return \&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; my $f = sub { my $i = shift; return $i+$n; }; return $f; }
And this is the main reason why closures in Perl are usually anonymous.

Replies are listed 'Best First'.
Re^2: Nailing down closures!
by blazar (Canon) on Dec 06, 2005 at 09:25 UTC
    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:
    This may be of some relevance here...