Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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; }}

In reply to Re: file handle limitation of 255 by TedPride
in thread file handle limitation of 255 by radnus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found