#!/usr/bin/perl
# Michael Tartaglia
# Basic polling program: The question was "Should Dihydrogen Monoxide be banned?"
# The majority said yes, and little did they know that "dihydrogen monoxide"
# is "water."
# This program displays a case, and, if the user hadn't already voted, asks for
# an opinion. The results are saved and displayed.
use CGI ":standard";
require template;
template::startHTML("Freelance Program","Poll");
open (POLL, ";
close(POLL);
@results = check(\@results); # SEND RESULTS TO 'CHECK' FOR CLEANING
my $whoVisited = $results[3]; # SEE WHO HAD TAKEN POLL ALREADY
my $whoIsHere = $ENV{'REMOTE_ADDR'}; # SEE WHO IS TAKING POLL NOW
# DISPLAY TITLE
print "The Dihydrogen Monoxide Crisis
";
# PRESENT CASE TO USER IF USER HAS NOT VOTED
if ($ENV{'QUERY_STRING'} eq "" || $whoVisited =~ /$whoIsHere/) {
print <
Thank you for taking the time to take this important poll. The
big companies are at it again and once again the environment
suffers.
The chemical responsible for the environmental harm is known as
dihydrogen monoxide. Dispite the information available
about this chemical - namely that it is potentially lethal if
inhaled - it is still being produced, and billions of gallons
of the liquid form of dihydrogen monoxide are dumped into
various lakes, streams, rivers, and oceans around the world.
The environmental danger is very real, though it goes widely
unreported. Every year, the number of flora with dihydrogen
monoxide-related root rot and the number of fauna which
unknowingly ingest the chemical increases. There is no knowing the
extent of the damage, simply because there is no way to track
every incident. However, insurance companies are beginning to
cover any property damage which may result from dihydrogen monoxide
over-production.
Those in support of dihydrogen monoxide praise its usefullness.
Farmers use large quantities of this chemical to prevent their
crops from drying out quickly under the sun. Despite the fact
that the chemical is responsible for putting young children in
serious danger (especially in summer), no deaths have been
reported in connection with farmers' using dihydrogen monoxide
and peoples' eating products treated with it.
EndOfHTML
} elsif ($whoVisited !~ /$whoIsHere/) { # IF USER SUBMITS RESPONSE OR ALREADY VOTED
print "Thank you for voting!
";
my ($toSave);
my $answer = param("answer");
print "";
if ($answer eq "yes") { # IF USER SELECTS "YES," SAVE RESULT AND PRINT MESSAGE
$results[0]++;
print "Those corporations will be happy to hear that
",
"you oppose the production of dihydrogen monoxide,
",
"better known as water.
";
} elsif ($answer eq "no") { # " " " NO, SAVE RESULT, ET CETERA
$results[1]++;
print "It is a good thing you do not oppose the production
",
"of dihydrogen monoxide, better known to you and me
",
"as aqua, or water.
";
} elsif ($answer eq "maybe") { # " " " NO, SAVE RESULT, ET CETERA
$results[2]++;
print "There is no harm in being unsure. The answer to the
",
"question, Should dihydrogen monoxide, or \"water,\"
",
"be halted? is a tough one to decide.
";
}
print "
";
$results[3] = $whoIsHere . " " . $whoVisited; # ADD CURRENT VOTER TO LIST
$toSave = "$results[0]\n$results[1]\n$results[2]\n$results[3]\n";
open (POLL, ">poll.txt"); # SAVE ALL INFORMATION TO TEXT FILE
flock (POLL, 2);
print POLL $toSave;
close (POLL);
}
if ($whoVisited =~ /$whoIsHere/) { # ALERT USER WHO ALREADY TOOK POLL
print "Sorry, you have already taken this poll.
",
"Thank you again for your input.
";
} elsif ($ENV{'QUERY_STRING'} eq "") { # PRESENT CHOICES
print <
Do you think companies should cease
production of dihydrogen
monoxide?
EndOfHTML
}
# IF POLL WAS ALREADY TAKEN, PRESENT RESULTS
if ($ENV{'QUERY_STRING'} ne "" || $whoVisited =~ /$whoIsHere/) {
print "
Here are the current results:
Should ",
"dihydrogen monoxide
production be stopped?
";
my $yes = $results[0];
my $no = $results[1];
my $maybe = $results[2];
my $total = int($no + $yes + $maybe);
print "Total respondants: $total.
";
my $noP = int(($no/$total)*300); # "NO PERCENTAGE" FOR GRAPH
my $yesP = int(($yes/$total)*300); # "YES PERCENTAGE" FOR GRAPH
my $maybeP = int(($maybe/$total)*300); # "MAYBE PERCENTAGE" FOR GRAPH
# CONSTRUCT TABLE TO DISPLAY BAR GRAPH, WHERE EACH "BAR" IS A TABLE ITSELF
my $table = ""
. "| Yes | "
. " | $yes votes | | No | "
. " | $no votes | | Don\'t Know | "
. "";
print "$table";
}
template::endHTML();
# ENSURE THAT NUMBERS PASSED AS ARRAY ARGUMENT ARE VALID AND CLEAN
sub check
{
my @array = @{$_[0]};
foreach (0 .. 2) {
$array[$_] = 0 if (!defined($array[$_]));
$array[$_] =~ s/\n// if ($array[$_] =~ /\n/);
}
return @array;
}
|
|---|
|
|---|
|
|---|