Perl腳本實(shí)現(xiàn)遞歸遍歷目錄下的文件
更新時間:2015年04月14日 09:24:57 投稿:junjie
這篇文章主要介紹了Perl腳本實(shí)現(xiàn)遞歸遍歷目錄下的文件,本文直接給出實(shí)現(xiàn)代碼,代碼中包含明細(xì)注釋,需要的朋友可以參考下
#!/usr/bin/perl -w
use strict;
use File::Spec;
local $\ ="\n";#當(dāng)前模塊的每行輸出加入換行符
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]) {#若是目錄執(zhí)行以下操作
opendir $dh, $files[0] or die $!;#打開目錄句柄,若失敗打印錯誤信息
@_ = grep { /^[^\.]/ } readdir $dh;#過濾掉以"."和".."的文件,即UNIX下的隱藏文件
foreach (@_) {
push(@files, File::Spec->catfile ($files[0], $_));#連接目錄名和文件名形成一個完整的文件路徑:
}
closedir $dh;
}
#若是文件直接壓入數(shù)組@cases中
elsif ($files[0] =~ /\.t$/) {
push(@cases, $files[0]);
}
shift @files;
}
}
else {
@cases = ($options{single_case});
}
print $_ foreach @cases;#打印文件列表
相關(guān)文章
Windows和Linux系統(tǒng)下perl連接SQL Server數(shù)據(jù)庫的方法
這篇文章主要介紹了Windows和Linux系統(tǒng)下perl連接SQL Server數(shù)據(jù)庫的方法,本文詳細(xì)的講解了Windows和Linux系統(tǒng)中perl如何連接Microsoft SQL Server數(shù)據(jù)庫,需要的朋友可以參考下2014-10-10
Perl實(shí)現(xiàn)的Linux下socket代理服務(wù)器
這篇文章主要介紹了Perl實(shí)現(xiàn)的Linux下socket代理服務(wù)器,比較簡潔的一個版本,需要的朋友可以參考下2014-08-08
perl常量、多維數(shù)組及變量的初始化的實(shí)例代碼
perl常量、多維數(shù)組及變量的初始化的例子,供大家學(xué)習(xí)參考2013-02-02

