The kind of subroutine that can access all lexical variables that existed at the time we declared it is called a closure (a term borrowed from the world of mathematics). In Perl terms, a closure is just a subroutine that references a lexical variable that has gone out of scope.
闭包就是能引用在程序范围外的词法变量的子程序。
例:$count
#!/usr/bin/perl -w
use strict;
{
my $count = 10;
sub count_one{$count--};
sub count_two{$count};
}
count_one();
print 'we have seen', count_two() ,"counts!\n"
阅读(906) | 评论(1) | 转发(0) |