Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: file handle limitation of 255

by TedPride (Priest)
on Jul 15, 2010 at 05:13 UTC ( #849703=note: print w/ replies, xml ) Need Help??


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; }}


Comment on Re: file handle limitation of 255
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://849703]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (16)
As of 2015-07-03 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls