That's a good example of using code refs, but it's not a closure. There are no private variables (unless I missed something there).
The anonymous routine (blessed or not) refers to a lexically-scoped variable in its enclosing scope. That lexically-scoped variable is magically kept alive (in the anonymous subroutine) after that enclosing scope has been exited. That is what makes an anonymous subroutine a closure.
sub makeAnonymousSub {
sub { print "I am not a closure\n" }
}
sub makeClosure {
my $x = shift;
sub { print "I keep $x alive, thus I'm a closure\n" }
}
Update: This is in agreement with perrin, btw. I'm just filling in some details...
|