Here's a question suitable for a fairly new Perl programmer. This is a bug in one of my programs from a few weeks ago. I've stripped down the code to the bare essentials. Can you find the bug?

The program creates some pages from a database, saves them locally, then uploads them to a remote server. The program takes a few seconds to run. After it exits, one file is always empty on the remote server. It is not empty on the local system.

my @built; my @files = get_files_to_build(); for (@files) { my $built = build_file($_); unless ($built) { warn "Could not build $_!\n"; next; } my $filename = File::Spec->catfile($path, $_); open(OUTPUT, "> " . $filename) or die "Can't open $filename: $!"; print OUTPUT $built; push @built, [ $filename, $_ ]; } my $ftp = Net::FTP->new($remotehost, Debug => 1) or die "Can't connect to $remotehost: $@\n"; $ftp->login($username, $password) or die "Couldn't authenticate!\n"; $ftp->cwd('www'); foreach my $updated (@built) { my ($location, $name) = @$updated; $ftp->put($name, $location) or warn "Couldn't put $name: $@\n"; } $ftp->quit();
(Apologies for any errors in transcription. This is a logic bug, not a typo.)

There are I came up with four possible solutions. One is very easy, the second works implicitly. The third may not work on every platform, and the fourth is crazy.

I'm including a couple of hints in HTML comments. I know several saints who would pick this out right off the bat, but let's leave this for people who don't normally speak up.

Hints follow.

Update: So far, ZZamboni and Albannach have both caught it. Good job! arturo also got it right, pointing out a legitimate typo that wasn't the bug. Then Spudnuts sent me a /msg, and I decided it would be okay for people to post their own answers. It might be good to stick them in HTML comments (<!-- comment here -->), though. So far no one's provided anything but the first and most direct fix.

tilly spotted two other typos and came up with four fixes. Abigail also gets a gold star, but if either of those two are novices, I'm secretly Jon Orwant.


In reply to 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.