Chinaunix
标题:
请大神帮忙看下
[打印本页]
作者:
b4and5
时间:
2014-10-19 20:18
标题:
请大神帮忙看下
use strict;
sub dir_walk {
my ($top, $code) = @_;
my $DIR;
$code->($top);
if (-d $top) {
my $file;
unless (opendir $DIR, $top) {
warn "Couldn't open directory $top: $!; Skipping.\n";
return;
}
while ($file = readdir $DIR) {
next if $file eq '.' || $file eq '..';
dir_walk("$top/$file", $code);
}
}
}
sub print_dir {
print $_[0], "\n";
}
dir_walk('.', \&print_dir);
复制代码
dir_walk 遍历目录内的文件和子目录...
假定当前目录.内只有文件,没有子目录,执行该脚本后会打印出该当前目录内的所有文件列表。
我不明白sub print_dir为什么能够打印出来当前目录下的文件列表。
不是就应该单单打印出一个.吗?
谢谢
作者:
b4and5
时间:
2014-10-19 23:02
回复
1#
b4and5
不好意思,我想明白了。
不知道怎么删除帖子。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2