I guessed the "problem" from just reading the description, hardly needed to look at the code. The problem comes from not closing the filehandle. Then the buffer isn't flushed, and hence the file is empty. You will never have more than one file suffering from this problem, and it's always the last file you wrote - this is because opening the same handle closes it first (if it was open). Good thing you didn't localize the handle!

One way of solving it is by using an autovivified scalar as handle:

for (@files) { ... stuff ... open my $output => ">", $filename or die "...."; print $output $built; push @built => [$filename => $_]; }
then when $output goes out of scope, that is, when the for block reaches the end, the file is automatically closed.

It is slightly better to do an explicit close yourself, as that allows you to inspect the return value of close. A close might fail (filesystem full, for instance).

-- Abigail


In reply to Re: Novice Quiz: File is created locally, uploaded empty by Abigail
in thread Novice Quiz: File is created locally, uploaded empty by chromatic

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.