#!/usr/bin/perl -w push(@INC,"/www/cgi-bin"); require 5.003; require "cgi-lib.pl"; #-------------------------- What This Does ---------------------------- # The new ingredient here is that we (finally) record the vote. We # keep the vote tally as if it were a mathematical function. Thus if # Gore has 72 votes, then $tally{Gore} returns the value 72. This # data for all the candidates is stored in what Perl calls a "hash". # New items: in Preliminaries, RecordVote (replaces RecordWhoVoted), and Thanks. #---------------------------- Configure ---------------------------- $vote = "http://www.math.upenn.edu/~kazdan/210/vote/vote5.html"; #Election Form $data_dir = "/www/cgi-bin/210/vote"; # Directory where data is kept $voted = "$data_dir/voted"; # those who already voted # The next vote tally file will have only one line. That line has the # vote data contained in %tally shown in the "What this does" section above. $votefile = "$data_dir/Votes"; # The lists of candidates: @candidates = ("Bush","Gore","someone else","undecided"); #---------------------------- Main Program ---------------------------- &ReadParse; print &PrintHeader; &Preliminaries; # If something is missing, print an error message and return to form. if (($in{lastname} eq "") || ($in{firstname} eq "") || ($in{"pres"} eq "")) { &missing; } else { # Their ballot is complete. OK unless voted already. &AlreadyVoted; if ( $alreadyvoted ne "yes") { &RecordVote; # Record vote, add name to voted list &Thanks; # Thanks and give current tally. &ClosePage; } } # end Main Program #--------------------------------------------------------------- sub Preliminaries { # Next is a useful "Return to the Form" button. $header = '
Return to Voting Forms Page
'; # Initialize the tally array using the data from the votefile: open(VOTES, "$votefile") || die "Can't open $votefile\n"; # Read it's one line of data. This gives the @votes array while () { chomp; # Removes the line ending (Line Feed). @votes = split(/,/); # Split the input at each comma } close (VOTES); # Now we can define the tally array assigning the votes to the candidates: $k=0; foreach $candidate (@candidates) { $tally{$candidate} = $votes[$k]; $k++; # increment k, so k -> k+1 } } # end Preliminaries #--------------------------------------------------------------- sub missing { # If something is missing, print an error message and return to form. # The only trick here is how to switch to a new URL after giving # 3 seconds to read the error message. The Election 2000 error $header  
To vote, you must include your First and Last names as well as your Vote.

In a few seconds your Election Ballot will be returned. error } # end missing #--------------------------------------------------------------- sub AlreadyVoted { # Did they already vote? We check. # Open the file listing those who already voted; die & warn if can't open. open(VOTED, "$voted") || die "Can't open $voted\n"; # Look through the list line by line to locate this voter. while () { # This reads VOTED one line at a time until the end. if ($_ eq "$in{lastname},$in{firstname}\n") { # if this person is found print "Our lists show you have already voted. Only one vote per person is allowed.\n"; $alreadyvoted = "yes"; last # Stop searching if the person is found. close(VOTED); } } } # end AlreadyVoted #--------------------------------------------------------------- sub RecordVote { # Record how this person voted $tally{$in{pres}}++; # Increment the tally $newvotes = ""; # Initialize the string foreach $candidate (@candidates) { $newvotes = "$newvotes$tally{$candidate},"; } `echo "$newvotes" > $votefile`; # Record that this person has voted # We could use the Unix "echo" command: # `echo "$in{lastname},$in{firstname}" >> $voted`; # Instead we use only internal perl commands: open(VOTED, ">>$voted") || die "Can't open $voted\n"; print VOTED "$in{lastname},$in{firstname}\n"; close(VOTED); } # end RecordVote #--------------------------------------------------------------- sub Thanks { # Say thanks and give voting summary print <Election 2000 $header  

To:   $in{firstname} $in{lastname}

Thank you for voting. (One more vote for "$in{pres}").

Here is the latest tally:

ok # Print the latest vote tally. foreach $candidate (@candidates) { print "
$candidate $tally{$candidate}"; } print "
"; } # end Thanks #--------------------------------------------------------------- sub ClosePage { # This closes the HTML page. print < end } # end ClosePage #---------------------------------------------------------------