2012-08-14 34 views
0

我已經上市目錄中的所有文件,下面的代碼,我有路徑解決麻煩,我的目錄是爲* 的/ tmp/ *,基本上我想要的文件這些目錄位於tmp目錄中。但我不允許使用*,你有什麼想法嗎?相對尋址在Perl中,使用開放DIR

my $directory="*/tmp/*/"; 
opendir(DIR, $directory) or die "couldn't open $directory: $!\n"; 
my @files = readdir DIR; 
foreach $files (@files){ 
    #... 
} ; 

closedir DIR; 

回答

2

執行opendir不能使用通配符工作

爲了您的任務存在有點難看,但工作液

my @files = grep {-f} <*/tmp/*>; # this is equivalent of ls */tmp/* 
# grep {-f} will stat on each entry and filter folders 
# So @files would contain only file names with relative path 
foreach my $file (@files) { 
    # do with $file whatever you want 
} 
+0

它的工作:) thx – shaq 2012-08-14 15:45:10

+0

試着玩\ */tmp/\ * * – CyberDem0n 2012-08-14 15:47:17

+0

我改了*/tmp/ – shaq 2012-08-14 15:48:19

1

沒有通配符和*通配符:

use 5.010; 
use Path::Class::Rule qw(); 
for my $tmp_dir (Path::Class::Rule->new->dir->and(sub { return 'tmp' eq (shift->dir_list(1,1) // q{}) })->all) { 
    say $_ for $tmp_dir->children; 
} 
+0

根本不懂這段代碼 – shaq 2012-08-14 15:45:34

+1

我也沒有,現在我做了。偉大的模塊。 @daxim dir_list在空目錄中返回undefined,我用(shift-> dir_list(1,1)//'') – 2012-08-14 17:11:08

+0

好的編輯答案 – daxim 2012-08-14 17:12:40