perl中的相对寻址,使用opendir

perl中的相对寻址,使用opendir,perl,opendir,relative-addressing,Perl,Opendir,Relative Addressing,我有下面列出目录中所有文件的代码,路径寻址有问题,我的目录是*/tmp/*,基本上我想要tmp目录中的文件。但是我不允许使用*,你知道吗 my $directory="*/tmp/*/"; opendir(DIR, $directory) or die "couldn't open $directory: $!\n"; my @files = readdir DIR; foreach $files (@files){ #... } ; closedir DIR; opendir无法使

我有下面列出目录中所有文件的代码,路径寻址有问题,我的目录是*/tmp/*,基本上我想要tmp目录中的文件。但是我不允许使用*,你知道吗

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

closedir DIR;

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
}
my@files=grep{-f};#这相当于ls*/tmp/*
#grep{-f}将统计每个条目和筛选文件夹
#所以@files将只包含具有相对路径的文件名
foreach my$文件(@files){
#你想用$file做什么都行
}

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
}
my@files=grep{-f};#这相当于ls*/tmp/*
#grep{-f}将统计每个条目和筛选文件夹
#所以@files将只包含具有相对路径的文件名
foreach my$文件(@files){
#你想用$file做什么都行
}

不带通配符和
*
通配符:

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;
}

不带通配符和
*
通配符:

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;
}

我通常使用
glob“*/tmp/*”
而不是
,因为我发现它很少混淆其他Perl成员(长期使用Perl的人似乎常常忘记该语法的作用)。甚至Perl本身有时也会混淆
是指还是。在我的书中,总是把它拼出来是一种很好的风格。我通常使用
glob“*/tmp/*”
而不是
,因为我发现它不太容易让其他Perl成员感到困惑(长期的Perl程序员似乎常常忘记了语法的作用)。甚至Perl本身有时也会混淆
是指还是。在我的书中,总是把它说清楚是一种很好的风格。我也没有,现在我知道了。很棒的模块@daxim dir_list在空dir上返回undefined,我使用了(shift->dir_list(1,1)/'')也没有,现在我使用了。很棒的模块@daxim dir_list返回未定义的空dir,我使用了(shift->dir_list(1,1)/“”)