There are a few points here.
- To upload a file, the HTML form needs to have a 'multipart/form-data' encoding type - this is done by 'start_form(-enctype=>'multipart/form-data'), instead of just start_form()'
- while (<$filename>) {print;} will 'empty' the filehandle. As I explained here, CGI.pm gives you a *handle* to the file it uploaded-and-decoded-and-saved-as-a-tmpfile, allowing you to read from it and write the data to somewhere else (OUTFILE).
(For that reason, I generally call it $filehandle). You've been confused by the CGI docs here - that bit of example code is actually *two* examples, as evidenced by the comments - one to read-and-print a text file, one to read-and-save a binary file. you want the second one - you can throw away that while<$filename> structure.
- You open'ed OUTFILE, so you close OUTFILE, not $filename. $filename is something 'lent' to you by CGI.pm - don't worry - it'll take care of it itself :)
Hope this clears some stuff up.
Ben