- 论坛徽章:
- 0
|
- 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为什么能够打印出来当前目录下的文件列表。
不是就应该单单打印出一个.吗?
谢谢
|
|