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 回答