37,720
社区成员
发帖
与我相关
我的任务
分享
{
my $count = 0;
sub callback { print ++$count; };
sub get_count{return $count;};
}
&callback(); //此时, $count = 1
&callback(); //此时, $count = 2
use File::Find;
sub create_find_callbacks_that_sum_the_size {
my $total_size = 0;
return(sub { $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"); //寻找bin目录下所有的文件,并对找到的所有文件执行回调函数$count_em
my $total_size = &$get_results( );
print "total size of bin is $total_size\n"
这段代码用于计算某个目录下所包含的所有文件的大小之和. #define int (* SUM)(int);
int get_sum(int filesize);
{
static int count = 0;
count += filesize;
return count;
}
bool find(SUM callback, char *path, int pathLen, int *total_size )
bool find(SUM callback, char *path, int pathLen, int *total_size )
{
bool result = false;
for(...) //寻找path下的每一个文件
{
...
if (file is find)
{
total_size = callback(filesize); //get_sum取代形参callback
result = true;
}
}
return result;
}
int totalsize = 0;
find(get_sum, path, pathlen, &totalsize);
#define int (* SUM)(int, bool);
int get_sum(int filesize, bool ret = false);
{
static int count = 0;
if (!ret)
{
count += filesize;
}
return count;
}
bool find(SUM callback, char *path, int pathLen)
{
bool result = false;
for(...) //寻找path下的每一个文件
{
...
if (file is find)
{
callback(filesize); //get_sum取代形参callback
result = true;
}
}
return result;
}
find(get_sum, path, len);
int totalsize = get_sum(anyinteger, true);
use File::Find;
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");
BEGIN {
my $countdown = 10;
sub count_down { $countdown-- }
sub count_remaining { $countdown }
}