Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69668
  • 博文数量: 13
  • 博客积分: 247
  • 博客等级: 二等列兵
  • 技术积分: 138
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-02 18:17
文章分类

全部博文(13)

文章存档

2015年(1)

2014年(1)

2013年(1)

2012年(2)

2011年(8)

我的朋友

分类: 系统运维

2011-08-10 22:21:30

1. Closures
When dereferenced, a subroutine reference can see all visible lexical variables when the reference to the subroutine is taken.
  1. use File::Find;
  2. my $total_size = 0;
  3. find(sub { $total_size += -s if -f }, '.');
  4. print $total_size, "\n";
2.$File::Find::name holds file name found relative to the starting search directory.
$_ holds file name relative to current working directory when callback subroutine is called.
 
3.we call the find routine with two parameters: a reference to an anonymous subroutine and the starting directory.
 
4.What is Closure in Perl?
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.
 
5.Closure Variable as Inputs
get in an input argument,and store it in a lexical variable,return subroutine reference reference this variable to build input closure.
 
 
6.Closure Variable as static local variable
we can declare Closure variable like static local variable in C by declaring
both subroutine and lexical variable within a naked block. Example as follows:
  1. {
  2.      my $count = 0;
  3.      sub increase_count{$count++}
  4.      sub count_so_far{return $count}
  5. }

because named subroutine has package scope,so named subroutine using lexical variables which went out of scope becomes Closure.(reference counting still takes effect).
So ,now this $count behaves like a static variable. The value retains between calls to increase_count subroutine,and can be changed only by increase_count.
 
When speaking of using Closure Variable as static local variable,one thing we should make clear is that the declaration of Closure Variable happens in compile time ,while the assignment happens during run time.

The declaration block should be put in the beginning of the block ,without any call to subroutine defined in the block behind it.
If we count using the following code ,it won't work as we think.
  1. &count_down;
  2. print “we have counted to ",&count_so_far ," so far\n";
  3. {
  4.     my $count = 10;
  5.     sub count_down{$count--};
  6.     sub count_so_far{return $count};
  7. }
  8. &count_down;
  9. &count_down;
  10. print “we have counted to ",&count_so_far ," so far\n";
the first should print  “we have counted to -1 so far”
the second should print “we have counted to 8 so far”
 
let's see what's happened:
before the first call of count_down and count_so_far subroutine ,$count has been
declared during the compile time but not yet assigned to the value “10”,so it was
undefined. So calling count_down just made the value stored in the memory address of  count to be --0 = -1.
the second and third call to count_down happened after the assignment of $count,so it printed what we supposed.
阅读(1395) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

regansong2011-08-10 22:27:57

小松加油