Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2014-09-22 08:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (184 votes), past polls