全部博文(43)
分类: Python/Ruby
2011-05-14 18:47:47
Closure在数学上的闭包.说真的,返东西的概念我还真不怎么懂,搜了下也是看的我于里雾里的.
来看看书上的原文定义:
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 fromt he world of mathematics).In Perl terms,a closure is just a subroutine that references a lexical variable that has gone out of scope.
大概来说,在Perl里面,闭包其实是一种特殊的子程序.返个子程序引用了一个词法变量,而返个词法变量已经离开其作用域了.
连起来说就是:闭包是一个引用了已经离开其作用域的词法变量的子程序-_-返个例子中,在nake code block外声明了一个变量$callback,并在nake code block中定义成一个匿名子程序的引用.返个匿名子程序中,使用了一个在返个nake code block中定义的词法变量$count.
在离开nake code block后,匿名子程序以回调形式被调用.