【发布时间】:2011-10-21 04:09:45
【问题描述】:
我正在设置一个包含文件句柄的哈希引用。
我的输入文件的第四列包含一个标识符字段,我用它来命名文件句柄的目标:
col1 col2 col3 id-0008 col5
col1 col2 col3 id-0002 col5
col1 col2 col3 id-0001 col5
col1 col2 col3 id-0001 col5
col1 col2 col3 id-0007 col5
...
col1 col2 col3 id-0003 col5
我使用 GNU 核心实用程序来获取标识符列表:
$ cut -f4 myFile | sort | uniq
id-0001
id-0002
...
此列中可以有超过 1024 个唯一标识符,我需要为每个标识符打开一个文件句柄并将该句柄放入哈希引用中。
my $fhsRef;
my $fileOfInterest = "/foo/bar/fileOfInterest.txt";
openFileHandles($fileOfInterest);
closeFileHandles();
sub openFileHandles {
my ($fn) = @_;
print STDERR "getting set names... (this may take a few moments)\n";
my $resultStr = `cut -f4 $fn | sort | uniq`;
chomp($resultStr);
my @setNames = split("\n", $resultStr);
foreach my $setName (@setNames) {
my $destDir = "$rootDir/$subDir/$setName"; if (! -d $destDir) { mkpath $destDir; }
my $destFn = "$destDir/coordinates.bed";
local *FILE;
print STDERR "opening handle to: $destFn\n";
open (FILE, "> $destFn") or die "could not open handle to $destFn\n$!\n";
$fhsRef->{$setName}->{fh} = *FILE;
$fhsRef->{$setName}->{fn} = $destFn;
}
}
sub closeFileHandles {
foreach my $setName (keys %{$fhsRef}) {
print STDERR "closing handle to: ".$fhsRef->{$setName}->{fn}."\n";
close $fhsRef->{$setName}->{fh};
}
}
问题是我的代码相当于id-1022:
opening handle to: /foo/bar/baz/id-0001/coordinates.bed
opening handle to: /foo/bar/baz/id-0002/coordinates.bed
...
opening handle to: /foo/bar/baz/id-1022/coordinates.bed
could not open handle to /foo/bar/baz/id-1022/coordinates.bed
0
6144 at ./process.pl line 66.
Perl 中我可以打开或存储在哈希引用中的文件句柄数量是否有上限?还是我在其他地方又犯了一个错误?
【问题讨论】:
-
Perl 没有限制,但你的操作系统肯定有。 (似乎是 1024。STDIN+STDOUT+STDERR+1021。)限制可能是可配置的。顺便说一句,你应该打印
$!,而不是$?。 -
perldoc.perl.org/FileCache.html FileCache 是一个标准模块,应该允许您超过操作系统对打开文件的限制。