我正在設置一個包含文件句柄的散列引用。我可以在Perl中打開的文件句柄的數量是否有限制?
我輸入文件的第四列包含了我使用來命名文件句柄的目的地標識符字段:
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)。這個限制可以是可配置的。順便說一句,你應該打印'$!',而不是'$?'。 – ikegami
你是對的,壞的錯字。 –
http://perldoc.perl.org/FileCache.html FileCache是一個標準模塊,應該允許您超出打開文件的操作系統限制。 – d5e5