免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5075 | 回复: 6
打印 上一主题 下一主题

请教 closure是什么意思? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-22 16:06 |只看该作者 |倒序浏览
  1. use File::Find;

  2. my $callback;
  3. {
  4.   my $count = 0;
  5.   $callback = sub { print ++$count, ": $File::Find::name\n" };
  6. }
  7. find($callback, '.');



复制代码
Here, we declare a variable to hold the callback. We cannot declare this variable within the naked block (the block following that is not part of a larger Perl syntax construct), or Perl will recycle it at the end of that block. Next, the lexical $count variable is initialized to 0. We then declare an anonymous subroutine and place its reference into $callback. This subroutine is a closure because it refers to the lexical $count variable.
Intermediate.Perl,第7.4节 , closure没看懂啊!求各位指教。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2010-03-22 16:37 |只看该作者
Here, we declare a variable to hold the callback. We cannot declare this variable within the naked b ...
Ray001 发表于 2010-03-22 16:06



    perlref 中有讲,可以看看。

论坛徽章:
0
3 [报告]
发表于 2010-03-22 16:38 |只看该作者
http://baike.baidu.com/view/648413.htm
呵呵找了个中文版,谢谢版主。

论坛徽章:
0
4 [报告]
发表于 2010-03-22 17:15 |只看该作者
5.10中直接引入了state,这个和closure是一样的

  1. sub {
  2.     state $x =1;
  3.     print $x++,"\n";
  4. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-03-22 17:31 |只看该作者
本帖最后由 xiaojao 于 2010-03-22 17:33 编辑

Larry Wall把闭包描述成只是一个带有数据匿名过程。

论坛徽章:
0
6 [报告]
发表于 2010-03-22 19:16 |只看该作者
我在这个帖子里有过解释:

http://bbs.chinaunix.net/viewthr ... p;extra=&page=2

论坛徽章:
0
7 [报告]
发表于 2010-08-13 09:15 |只看该作者
我也正在看~~~我的理解是相当于C++的local static吧~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP