#!/usr/bin/perl
use CGI;
use Fcntl; # import the LOCK_ constants
$query = new CGI;
@names = $query->param;
$name = $query->param("name");
$hero=$query->param("radio");
# print header
print "Status: 200 Content follows\n";
print "Content-type: text/html\n\n";
# print start stuff
print "
Survey Results
\n";
# check to see if this person has voted before
open(FILE, ") {
chomp($line);
($fname, $fhero) = split(",", $line);
if ($fname eq $name) {
print "You already voted. Rejected\n";
exit(0);
}
$count{$fhero}++;
$totalvotes++;
}
$count{$hero}++; # count this vote too
$totalvotes++;
#log the vote
#if (!open(FILE, ">>survey.dat))
#{
# print "file was bad";
# die "File was bad";
#}
open(FILE, ">>survey.dat") || ( print "File perms bad" && die "Unable to open file");
#flock(FILE, 0) || die "Unable to lock file";
#seek(FILE, 0, 2) || die "Unable to seek to end of file";
print FILE "$name,$hero\n";
close(FILE);
print "$name, you voted for $hero
\n";
print "
\n";
print "Superhero | Votes |
\n";
foreach $hero (sort keys %count) {
print " $hero | $count{$hero} | ";
for($i = 1; $i <=$count{$hero}; $i++) {
print " ";
}
print " |
\n";
}
print " TOTAL | $totalvotes |
";
print "
\n";