If that is the case - combine the two methods.
  1. Buffer the strings up to say 10k or more.
  2. Once they hit that size - look for an open, or cache a new filehandle.
  3. Print out the buffered string for the file and clear the buffer.
  4. Finish off by flushing remaining buffers.
my @a=qw(random brilliant braindead); print $a[rand(@a)];

In reply to Re^4: Performance Trap - Opening/Closing Files Inside a Loop by Rhandom
in thread Performance Trap - Opening/Closing Files Inside a Loop by Limbic~Region

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