Perl中的闭包(closure)_第1页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论