0

ディレクトリ内のすべてのファイルをリストするための次のコードがあります。パスのアドレス指定に問題があります。私のディレクトリは* /tmp/ *です。基本的に、tmpディレクトリのディレクトリにあるファイルが必要ですが、許可されていません。 * を使用してください。

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

closedir DIR;
4

2 に答える 2

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
}
于 2012-08-14T15:22:55.427 に答える
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;
}
于 2012-08-14T15:36:14.140 に答える