> If an anonymous sub doesn't close over an external variable then it isn't a closure.
And if a named sub does, it's also a closure.
Indeed two unrelated concepts.
UPDATE
I just checked the etymology, the fact that the bound variables are not accessible from the outside of their defining scope makes the sub "closed".
{
my $state = 42;
sub closed_sub {
return $state++;
}
}
say closed_sub(); # 42
say closed_sub(); # 43
Contrary to subs binding global variables, which are "open".
{
our $state = 42;
sub open_sub {
return $state++;
}
}
say open_sub(); # 42
$main::state = 666;
say open_sub(); # 666
(of course Perl allows you to bind both kinds, so it's not an exclusive attribute here)
see references from WP:
|