in reply to Novice Quiz: File is created locally, uploaded empty

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

Replies are listed 'Best First'.
Re (tilly) 2: Novice Quiz: File is created locally, uploaded empty
by tilly (Archbishop) on Jun 22, 2001 at 05:04 UTC
    Actually if you localize the handle, Perl will close it for you as the handle goes out of scope. So that is a perfectly fine way to solve this problem.
Re: Re: Novice Quiz: File is created locally, uploaded empty
by John M. Dlugosz (Monsignor) on Jun 23, 2001 at 01:09 UTC
    using an autovivified scalar as handle

    I like that. So there is no real need to "use symbol", is there?

      Well, autovivified scalars as filehandles is new in 5.6. You might want to use use Symbol; (note the capitalation) in older Perls.

      -- Abigail