OnionKnight has asked for the wisdom of the Perl Monks concerning the following question:

Hay guys I'm new to Perl and I'm using it for CGI-scripting with mod_perl and I'm having some problems. The command-line Perl compiler has no problem compiling it but when I open the script in my browser I get 500 error a 500 Internal Server Error. I am trying to make a simple file uploader and the page was working before I added it.
use strict; use CGI qw(:standard); use Fcntl qw(:DEFAULT :flock :seek); use CGI::Carp qw(fatalsToBrowser warningsToBrowser); sub filesize { my $size = $_[0]; my $i = 0; my @suf = ("B", "KiB", "MiB", "GiB", "TiB"); for (; $size >= 1024; $i++) { $size /= 1024; } return sprintf("%.1f %s", $size, $suf[$i]); } my ($password, $file) = (param("password"), param("file")); if ($ENV{"REQUEST_METHOD"} eq "POST" && defined $file && defined $pass +word) { use constant UPLOADPUB => "C:\\Program Files\\Apache Group\\Apache +2\\htdocs\\upload\\"; use constant UPLOADDIR => "C:\\Documents and Settings\\Onion Knigh +t\\Desktop\\"; use constant MAXSIZE => 1048576; seek($file, 0, SEEK_END); my $size = tell($file); seek($file, 0, SEEK_SET); if ($password eq "") { if (sysopen(FILE, UPLOADDIR.$file, O_WRONLY | O_CREAT | O_TRUN +C)) { flock(FILE, LOCK_EX); binmode FILE; my $buffer; while (read($file, $buffer, 512)) { print FILE $buffer; } print " $file uploaded successfully! (" +, &filesize($size), ")"; close(FILE); } else { print " Couldn't write file to disk."; } } elsif ($password eq "guest") { if ($size < MAXSIZE) { if ($file !~ /^\./) { unless (-e UPLOADPUB.$file) { if (sysopen(FILE, UPLOADPUB.$file, O_WRONLY | O_CR +EAT | O_TRUNC)) { flock(FILE, LOCK_EX); binmode FILE; my $buffer; while (read($file, $buffer, 512)) { print FILE $buffer; } print " $file uploaded succ +essfully! (", &filesize($size), ")"; close(FILE); } else { print " Couldn't write file + to disk."; } } else { print " File exists already."; } } else { print " Filename not allowed."; } } else { print " Filesize too big!"; } } else { print " Password incorrect."; } } else { print " <br />"; }
I didn't include any of the HTML-dumping prints because those are unnecessary.

Update: Sorry for writing bad code guys, I improved on it a little. As for headers, yes, I have them but like I said I didn't include any of the functions that print huge chunks of HTML. I tested commenting out the "use strict;" and it worked then! I wonder why. Also, the upload script doesn't work. It seems to me that I don't know how to handle uploaded files in the first place, what am I doing wrong? And why does strict prevent the site from generating?

Update 2: It works perfectly now! The file uploader, and I can use strict; without error. Exception being that the filesize is the size on disk rather than the size of the file. But I think that can be fixed.

Edited by Arunbear: Changed title from 'mod_perl mod_perl lol', as per Monastery guidelines

Replies are listed 'Best First'.
Re: Trying to make a simple file uploader CGI script
by Joost (Canon) on Aug 04, 2005 at 23:27 UTC
Re: Trying to make a simple file uploader CGI script
by bart (Canon) on Aug 05, 2005 at 08:21 UTC
    I don't see you output any headers. So, (part of) the actual body is treated as the headers, required ones (like the content-type) are missing, other lines are not recognized as headers... Of course it gives a server error.
Re: Trying to make a simple file uploader CGI script
by wink (Scribe) on Aug 05, 2005 at 08:23 UTC

    Need to give it a content-type. The browser needs to know what it's displaying so it can display it correctly. You can use CGI::header to provide this. Since you already use CGI qw(:standard), you can just do print header; somewhere near the top of your program.