如果你想處理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