广州明生堂生物科技有限公司


Perl脚本实现递归遍历目录下的文件

网络编程 Perl脚本实现递归遍历目录下的文件 06-21
#!/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 $!;#打开目录句柄,若失败打印错误信息
        @_ = grep { /^[^.]/ } readdir $dh;#过滤掉以"."和".."的文件,即UNIX下的隐藏文件
        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;#打印文件列表

Perl读写文件简单示例
!/usr/bin/perl-wusestrict;#print"pleaseinputastringn";#my$line=STDIN;#print$line;#wirteafileopen(FH,"aa.txt")ordie$!;printFH"hellon";#向文件写入内容printFH"OKn";close(FH);#openafileopen(FH,"

perl读写文件代码实例
#modeoperandcreatetruncate#read#writeyesyes#appendyesCase1:Throwanexceptionifyoucannotopenthefile:usestrict;usewarnings;my$filename='data.txt';open(my$fh,':encoding(UTF-8)',$filename)ordie"Couldnotope

perl中使用signal(信号)实例
使用signal,能让你的程序功能更丰富。要在Linux下列出所有的signal,利用kill-l即可。下面是我机器上的输出(后面还有到64的没列出来):xuyang@xuyang-deskt


编辑:广州明生堂生物科技有限公司

标签:文件,目录,路径,实例,句柄