Esteemed Monks,

I have some code along the lines of:

$path = "/var/tmp/decode1"; opendir(DIR, $path); @files = grep { /\.dat$/ } readdir(DIR); closedir(DIR); foreach $file (@files) { open(INPUT, "$path/$file") or die "can't open file $file: $!"; # do some stuff close INPUT; }
If I have ~20 files in this directory no problem. But when I work on the order of a couple thousand files I reach my system limit.

Shouldn't the close, close the file descriptor, then when it loops to my next file, open a new file handle? I'm not explicitly forking. I would think that I'm only working on 1 file at a time. (Not all 1000 of them.) I guess this is not the case.

So instead of trying to use threading (which I couldn't get to work properly.), should I just fork children myself then wait for the child to come back? All I want to do is work on one file at a time in a directory of my choice.

TIA


In reply to Question on fork and close. by sbank

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.