
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、perl中的闭包(closure)什么是闭包,“this is a notion out of the lisp world that says if you define an anonymous fution in a particular lical context, it pretends to run in that context even when its call outse of the context.”【2】。在面对对象的语言里面,“a clore is a callable object that retains infoation from the scope in
2、it was created. from this definition, you can see that an inner class is an object-oriented closure, because it doesnt just contain each piece of information from the outer-class object ( the scope in which it was created ), but it automatically holds a reference back to the le outer-class object, w
3、here it has permission to manipulate all the members, even private ones.”【3】 先看这个例子: !/usr/bin/perl -w use sict; my $inc = 10; sub incr print $incn $inc+; incr(); incr(); prints: 11 这个例子解释命名函数默认是全局的,即使是定义在一个block里面。我们不能引用变量$inc,但是却可以调用函数。 !/usr/bin/perl -w use strict; sub make_incr my $inc = shift;
4、return sub print $incn $inc+ ; my $c1 = make_incr(10); my $c2 = make_incr(20); $c1- $c2- $c1- $c2- prints: 21这个例子我们看到了,perl的函数返回其实是一个匿名函数引用,这个就是magic所在了。这个也是perl如何实现闭包的。 !/usr/bin/perl -w use strict; sub exclaim my $prefix = shift; return sub print $prefix $_0!n ; my $batman = exclaim(indeed); my $r
5、obin = exclaim(holy); $robin- (mackerel); prints: holy mackerel! $batman- (robin); prints: indeed robin! 那么闭包有什么作用呢?以下摘自“learning perl objects, references moles”的第6章【1】: 使用一 在subroutine中返回subroutine的引用,通常作为回调函数: use file:find; sub create_callbacks_that_sum_the_size my $total_size = 0; return ( sub $
6、total_size += -s if -f , sub return $total_size ); my ( $count_em, $get_results ) = create_find_callbacks_that_sum_the_size(); find( $count_em, bin ); my $total_size = $get_results(); print total size of bin is $total_size n 这段代码用于计算某个名目下所包含的全部文件的大小之和. 使用二 用法闭环变量作为输入,用作函数生成器,来生成不同的函数指针: !/usr/bin/pe
7、rl -w use strict; sub print_bigger_than my $minimum_size = shift; return sub print $file:find:name/n if -f and -s = $minimum_size ; my $bigger_than_1024 = print_bigger_than(1024); find( $bigger_than_1024, bin );print_bigger_than在这里相当于一个函数生成器,不同的输入变量可以生成不同的函数指针.这里生成了一个可以打印出文件大小大于1024字节文件名的回调函数. 使用三 作
8、为静态局部变量用法,提供了c语言静态局部变量的功能: begin my $countdown = 10; sub count_down $countdown- sub count_remaining $countdown 这里用到了关键字begin. begin的作用就是,当perl编译完这段代码之后,停止当前编译,然后挺直进入运行阶段,执行begin块内部的代码.然后再回到编译状态, 继续编译剩余的代码. 这就保证了无论begin块位于程序中的哪个位置,在调用count_down之前,$countdown被确保初始化为10. 最后附上一个相当cool的例子,来在“perl best prac
9、tices”: generate a new ing routine whose name is the string in $sub_name and which sorts on keys extracted by the subroutine referred to by $key_sub_ref sub make_sorter my ( $sub_name, $key_sub_ref ) = _; create a new anonymous subroutine that implements the sort. my $sort_sub_ref = sub sort using t
10、he schwartzian transform. return map $_- 0 3. return original value sort $a- 1 $b- 1 2. compare keys map $_, $key_sub_ref- () 1. extract key, cache with value _; 0. perform sort on full arg list install the new anonymous sub into the callers namespace use sub:installer; caller- install_sub( $sub_name, $sort_sub_ref ); return; and then. make_sorter( sort_sha = sub sha512($_) ); make_sorter( sort_ids = sub /id:(d+)/ ); make_sorter( sort_len = sub len
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 58 选择性必修3 第九单元 第49讲 植物细胞工程
- 52 选择性必修2 第八单元 第43讲 生态系统的结构及其稳定性
- 出租车公司股权转让与车辆更新协议
- 买文具教学设计课件
- 湘美教学一年级下册课件
- 2024-2025学年安徽省蚌埠市高一下学期第六次联考政治试题及答案
- 家用纺织品市场推广中的情感化营销策略考核试卷
- 音乐教育中的乐器制作教学评估体系构建考核试卷
- 冷库泄漏处理考核试卷
- 身韵在舞蹈创作中的运用考核试卷
- 江苏省南京市六校联合体2024-2025学年高一下学期期末调研测试历史试题(含答案)
- 2025年法律职业资格考试民法专项练习卷:合同法真题解析及试题
- 玻尿酸介绍课件
- 2025至2030年中国电子束曝光系统行业市场研究分析及发展前景研判报告
- 2025中国心肌病综合管理指南要点解读课件
- 技术中心人员管理制度
- 2025年形势与政策课程期末考试复习试卷及答案
- 财产独立性专项审计报告模板3(清算审计报告模板)
- 2025年中考英语答题技巧与模式专题11阅读七选五(学生版+解析)
- 2024年个人信用报告(个人简版)样本(带水印-可编辑)
- 16J914-1 公用建筑卫生间
评论
0/150
提交评论