No one?
Well, ok... I have been reading like a bat outta hell and started working on this myself. The file is at
http://www.bartlett-family.net/cgi-bin/wordcount2.cgi so please look there to see it in action.
Now, I have completely rewritten it based on what I need to get done for this assignment. Could anyone help further? I'm kind of stuck and it's driving me nuts!
The page comes up fine when you open it in a browser, but if click on "Check it out", the browser goes to a server error screen (regardless of whether you select the default file, upload one, or do nothing at all). Looking at my Apache logs, I am shown the the error is a "Premature end of script headers: wordcount2.cgi" error. Can anyone please take a look at my code and tell me what they think might be causing this? I think I've been staring at it for too long, lol.
Code:
#!/usr/bin/perl
use CGI ':standard';
use CGI::Carp 'fatalsToBrowser';
if (!param) {print_form();} #Call Upload form if no file has been submitted
else {word_count();} #Call Word count routine if file has been submitted
#Routine displays HTML form that allows user to upload a text file or select default file to count
sub print_form {
print header;
print start_html(-title=>'Word Counter PERL program', -BGCOLOR=>'#f0f8ff');
print hr;
print h1("Word Counter Program");
print hr;
print start_multipart_form();
print "Please click the Browse button to upload a file of your choice or click in the box below to use the default file, The Raven by Edgar Allen Poe";
print br;
print filefield(-name=>'upload', -size=>40);
print br;
print checkbox(-name=>'check here to use The Raven', -value=>'off');
print br;
print br;
print submit(-label=>'Check it out!');
print reset;
print endform;
}
#Routine calculates the number of words, lines and characters in the user selected file
#Displays HTML table with results of the calculations
sub word_count {
my $char_count = 0; #Declare sub specific variables
my $line_count = 0;
my $word_count = 0;
my @words;
my $file = param('upload'); #Moves uploaded file into memory if entered
if(param('Default')){ #Check is user requested default file
open ($file, 'raven.txt') or die; #Open default text file
}
else {if (!$file) { #If no file was found return message to user
print "No file uploaded. Click the Back button on your browser to try again!",
return;
}
}
while (<$file>) { #Loop steps through text file one line at a time
$line_count++; #increments for each line in file
$char_count += length($_); #Counts number of characters in each line and adds to previous
@words = split; #Splits each line on whitespace and assigns to array
#$word_count += scalar(@words); #Counts elements in array and adds to previous total
foreach (split /\W+/) {
next if ($_ eq /\n/);
$word_count++;
$_ = lc($_);
$count{$_}++;
}
}
$indword_count = scalar(keys(%count));
print "idv words: $indword_count";
print #Displays results of counts
"<h2>Here are your results</h2>",
"<table border = 1 ><tr>",
"<th>File Name</th>",
"<th># of Lines</th>",
"<th># of Words</th>",
"<th># of Characters</th>",
"</tr>",
"<td>$file</td>",
"<td>$line_count</td>",
"<td>$word_count</td>",
"<td>$char_count</td>",
"</tr></table><br>",
"<p><a href='wordcount.cgi'>Return to Main Screen</a><br>";
print #Displays results of counts
"<h2>Here are your results</h2>",
"<table border = 1 ><tr>",
"<th>Word</th>",
"<th># of Occurences</th>",
"<th>% of Occurences</th>",
"</tr>";
foreach $word (sort keys %count) {
$percent = (($count{$word}/$word_count) * 100);
print
"<tr><td>$word</td>",
"<td>$count{$word}</td>",
"<td>";
printf "%2.3f", $percent;
print
"%</td></tr>";
# was seen $count{$word} times.<br>";
}
print "</tr></table></br>";
print "<br><br>";
print #Displays results of counts
"<h2>Here are your results</h2>",
"<table border = 1 ><tr>",
"<th>Word</th>",
"<th># of Occurences</th>",
"<th>% of Occurences</th>",
"</tr>";
foreach $word (sort {$count{$b} <=> $count{$a}} keys %count) {
$percent = (($count{$word}/$word_count) * 100);
print
"<tr><td>$word</td>",
"<td>$count{$word}</td>",
"<td>";
printf "%2.3f", $percent;
print
"%</td></tr>";
# was seen $count{$word} times.<br>";
#print "$word was seen $count{$word} times.<br>";
}
}
Thank you!