Update: if BrowserUK is correct, I can be safely ignored.

Opening and closing a file for each record is going to get you lousy performance. If the records are sorted by filename, try (untested):

my $openfile = ''; foreach my $file (@file) { my $filename = $file->{filename}; my $content = $file->{content}; if ($filename ne $openfile) { $openfile = $filename; open(FILE, "> $filename") or die "Can't open $filename $!"; } print FILE $content; } close(FILE) if $openfile;
Otherwise, sort by filename (untested):
for my $file (sort {$a->{filename} cmp $b->{filename}} @files) { ... }
You may get slightly better results saving all the records for a file in an array and then printing them together just before opening the next file (or the close at the end).

If you had a smaller number of files, I'd recommend a hash of filehandles instead:

foreach my $file (@files) { my $filename ... my $content ... if (!$fh{filename}) { open $fh{filename}, "> $filename" or die ... } print {$fh{filename}} $content; } foreach my $fh (keys %fh) { close($fh{$fh}); }

In reply to Re: Quickest way to write multiple files by ysth
in thread Quickest way to write multiple files by Anonymous Monk

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.