Chinaunix

标题: 请大神帮忙看下 [打印本页]

作者: b4and5    时间: 2014-10-19 20:18
标题: 请大神帮忙看下
  1. use strict;

  2. sub dir_walk {
  3.     my ($top, $code) = @_;
  4.     my $DIR;

  5.     $code->($top);

  6.     if (-d $top) {
  7.         my $file;
  8.         unless (opendir $DIR, $top) {
  9.             warn "Couldn't open directory $top: $!; Skipping.\n";
  10.             return;
  11.         }
  12.         while ($file = readdir $DIR) {
  13.             next if $file eq '.' || $file eq '..';
  14.             dir_walk("$top/$file", $code);
  15.         }
  16.     }
  17. }

  18. sub print_dir {
  19.        print $_[0], "\n";
  20.     }

  21. 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