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


In reply to Trying to make a simple file uploader CGI script by OnionKnight

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.