Perl script implements recursive traversal directory file _perl

Source: Internet
Author: User
Tags perl script
#!/usr/bin/perl-w use
strict;
Use File::spec;

Local $\ = "\ n"; #当前模块的每行输出加入换行符 my  

%options;

#目录路径
$options {single_case} = '/home/jiangyu/src/pl/example ';

  my @cases;
  if (-D $options {single_case}) {#判断目录是否存在 my
    @files;
    my $dh;
    Push (@files, $options {single_case});
    while (@files) {
      if (-D $files [0]) {#若是目录执行以下操作
        opendir $dh, $files [0] or Die $!; #打开目录句柄, if failure prints error message
        @_ = grep {/^[^\.] /} Readdir $dh; #过滤掉以 "." and ".." Files, that is, under Unix, the hidden files
        foreach (@_) {
          push (@files, File::spec->catfile ($files [0], $_)); #连接目录名和文件名形成一个完整的文件路径 :
        }
        closedir $dh;
      }
      #若是文件直接压入数组 @cases
      elsif ($files [0] =~/\.t$/) {
        push (@cases, $files [0]);
      }
      Shift @files;
    }
  }
  else {
    @cases = ($options {single_case});
  }


Print $_ foreach @cases; #打印文件列表

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.