http://qs321.pair.com?node_id=849703


in reply to file handle limitation of 255

Most of these sorts of problems can be best solved by telling us WHY - why, in this case, you want to open more than 255 file handles. If we assume for the sake of argument that you need to read one line at a time from each of thousands of files, what you could do is write a function to keep track of where you are in each file and close / open new files on a priority basis - something like the following hack:
use strict; my $limit = 250; ### Leaving a few for main program my $line; $line = readFile('inp1.txt'); $line = readFile('inp2.txt'); $line = readFile('inp3.txt'); ### etc flushFiles(); { my %files; ### Structure containing handle, position, etc. my $fcount; ### Number of open files my $opened; ### Sequential number to tell order opened sub readFile { my $file = $_[0]; my ($closer, $handle, $line); ### Need to open file, but too many open if ((!$files{$file} || !$files{$file}{'handle'}) && $fcount == $limit) { ### Find best file to close $closer = (sort { $b->{'open'} <=> $a->{'open'} || ### Must be open $a->{'accessed'} <=> $b->{'accessed'} || ### Accessed leas +t $a->{'opened'} <=> $b->{'opened'} ### Opened earlie +st } values %files)[0]; close $closer->{'handle'}; $closer->{'handle'} = undef; $closer->{'open'} = undef; $fcount--; } ### Need to open file, maybe seek old position if (!$files{$file}{'handle'}) { return if !open($handle, $file); $files{$file}{'handle'} = $handle; $files{$file}{'open'}++; seek($handle, $files{$file}{'position'}, 0) if $files{$file}{'position'}; $files{$file}{'opened'} = ++$opened if !$files{$file}{'opened'}; $fcount++; } else { $handle = $files{$file}{'handle'}; } $files{$file}{'accessed'}++; return if !($line = <$handle>); ### If we successfully read data from file $files{$file}{'position'} += length $line; return $line; } sub flushFiles { my $handle; for (values %files) { close $_->{'handle'} if $_->{'handle'}; } %files = (); $fcount = undef; }}