如果你想处理glob模式,使用glob
运营商将其展开。然后测试所有路径,将结果存储在散列中,然后返回散列。
sub FileExists {
my @param = map glob($_) => @{ shift @_ };
my %exists;
foreach my $file (@param) {
print $file, "\n";
$exists{$file} = -e $file;
}
wantarray ? %exists : \%exists;
}
然后说,你把它作为
use Data::Dumper;
my @arr = ('/tmp/test/test.*.con', '/usr/bin/a.txt');
my $result = FileExists(\@arr);
$Data::Dumper::Indent = $Data::Dumper::Terse = 1;
print Dumper $result;
采样运行:
$ ls /tmp/test
test.1.con test.2.con test.3.con
$ ./prog.pl
/tmp/test/test.1.con
/tmp/test/test.2.con
/tmp/test/test.3.con
/usr/bin/a.txt
{
'/tmp/test/test.3.con' => 1,
'/tmp/test/test.1.con' => 1,
'/usr/bin/a.txt' => undef,
'/tmp/test/test.2.con' => 1
}
我不知道,如果你在Windows或Unix的时候,但在Unix外壳通常手柄通配符扩展。因此,如果您运行'myprog.pl * .txt',您的程序将在目录中看到目录中的.txt文件列表(如果有的话)。为了避免这种扩展,你需要用引号将参数括起来:'myprog.pl“* .txt”' – Arkadiy 2010-08-04 14:05:59