Opinion surveys of users of the web document
Each user fills in the choices
The collective data must be stored between visits
Where? in the server disk!!
For instance the cgi-bin directory
$LOCK = 2; $UNLOCK =8; ... flock(<TAX_DATA>, $LOCK); # Locking acces to the file chomp($taxes = <TAX_DATA>); # # Processing of $taxes # flock(<TAX_DATA>, $UNLOCK); # unlocking acces to the file
<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd"> <!-- conelec.html A document to present the user with a consumer electronics purchasing survey form --> <html> <head> <title> Consumer Electronics Purchasing Survey </title> </head> <body> <form action = "http://www.cs.ucp.edu/cgi-bin/sebesta/conelec1.pl" method = "POST"> <h2> Welcome to the Consumer Electronics Purchasing Survey </h2> <br /> <h4> Your Age Category: </h4> <input type = "radio" name = "age" value = "b1025" checked > 10-25 <br /> <input type = "radio" name = "age" value = "b2640"> 26-40 <br /> <input type = "radio" name = "age" value = "b4160"> 41-60 <br /> <input type = "radio" name = "age" value = "o60"> Over 60 <br /> <br /> <h4> Your Gender: </h4> <input type = "radio" name = "gender" value = "female" checked> Female < br /> <input type = "radio" name = "gender" value = "male"> Male <br /> <br /> <h4> Your Next Consumer Electronics Purchase will be: </h4> <input type = "radio" name = "vote" value = "0"> Conventional TV <br /> <input type = "radio" name = "vote" value = "1"> HDTV <br /> <input type = "radio" name = "vote" value = "2"> VCR <br /> <input type = "radio" name = "vote" value = "3"> CD player <br /> <input type = "radio" name = "vote" value = "4"> Mini CD player/recorder <br /> <input type = "radio" name = "vote" value = "5"> DIVD player <br /> <input type = "radio" name = "vote" value ="6" checked> Other <br /> <br / > <input type = "submit" value = "Submit Order"> <input type = "reset" value = "Clear Order Form"> </form> <hr/> To see the results of the survey so far, click <a href = "http://www.cs.ucp.edu/cgi-bin/sebesta/conelec2.pl"> here </a>; </body> </html>
# conelec1.pl # This CGI program processes the consumer electronics survey # form and updates the file that stores the survey # and updates the file that stores the survey data, use CGI qw(:standard); # error - a function to produce an error message for the client # and exit in case of open errors sub error { print "Error - file could not be opened in conelec1.pl <br />"; print end_html(); exit(1); } # Begin main program # Get the form values my($age, $gender, $vote) = (param("age"), param("gender"), param("vote")); # Produce the header for the reply page - do it here so # error messages appear on the page print header(); # Set names for file locking and unlocking $LOCK = 2; $UNLOCK = 8; # Set $index to the line index of the current vote $index = 0; if ($gender eq "male") { $index = 4; } if ($age eq "b2640") { $index += 1 } elsif ($age eq "b4160") { $index += 2 } elsif ($age eq "o60") { $index += 3 } # Open and lock the survey data file open(SURVDAT, "<survdat.dat") or error(); flock(SURVDAT, $LOCK); # Read the survey data file, unlock it, and close it for ($count = 0; $count <= 7; $count++) { chomp($file_lines[$count] = <SURVDAT>); } flock(SURVDAT, $UNLOCK); close(SURVDAT); # Split the line into its parts, increment the chosen # device, and put it back together again @file_votes = split / /, $file_lines[$index]; $file_votes[$vote]++; $file_lines[$index] = join(" ", @file_votes); # Reopen the survey data file for writing and lock it open(SURVDAT, ">survdat.dat") or error(); flock(SURVEY, $LOCK); # Write out the file data, unlock the file, and close it for ($count = 0; $count <= 7; $count++) { $line = $file_lines[$count]; print SURVDAT "$line\n"; } flock(SURVDAT, $UNLOCK); close(SURVDAT); # Build the web page to thank the survey participant print start_html("Thankyou"), print "Thanks for participating in our survey <br /><br />\n"; print end_html();
# EZ table.pl table({-border => "border"}, caption("Sales Figures"), Tr( [tr(["Salesperson", "Mon", "Tues", "Wed", Thu","Fri"]), th("Mary").td(\@maysales), th("Freddie").td(\@freddiesales), th("Spot").td(\@spotsales), ] ) );
The survey has the following tasks:
#!/usr/local/bin/perl -w # conelec2.pl - display the survey results use CGI qw(:standard); # error - a function to produce an error message for the client # and exit in case of open errors sub error { print "Error - file could not be opened in conelec2.pl <br/>"; print end_html(); exit(1); } # Begin main program # Initialize file locking/unlocking parameter $LOCK = 2; $UNLOCK = 8; print header(); # Open, lock, read, and unlock the survey data file open(SURVDAT, "<survdat.dat") or error(); flock(SURVDAT, $LOCK); @vote_data = <SURVDAT>; flock(SURVDAT, $UNLOCK); # Create the beginning of the result web page print start_html("Survey Results"); print "<h2> Results of the Consumer Electronics Purchasing Survey </h2><br/ > \n"; # Split the input lines for females into age arrays @age1 = split(/ /, $vote_data[0]); @age2 = split(/ /, $vote_data[1]); @age3 = split(/ /, $vote_data[2]); @age4 = split(/ /, $vote_data[3]); # Add the row titles to the age arrays unshift(@age1, "10-25"); unshift(@age2, "26-40"); unshift(@age3, "41-60"); unshift(@age4, "Over 60"); # Make the column titles array @col_titles = ("Age Group", "Conventional TV", "HDTV", "VCR", "CD player", "MiniCD player/recorder", "DIVD player", "Other"); # Create the column titles in HTML by giving their address to the th # function and storing the return value in the @rows array @rows = th(\@col_titles); # Now create the data rows with the td function # and add them to the row addresses array push(@rows, td(\@age1), td(\@age2), td(\@age3), td(\@age4)); # Create the table for the female survey results # The address of the array of row addresses is passed to Tr print table({-border => "border"}, caption("<h3>Survey Data for Females</h3>"), Tr(\@rows) ); # Split the input lines for the males into age arrays @age1 = split(/ /, $vote_data[4]); @age2 = split(/ /, $vote_data[5]); @age3 = split(/ /, $vote_data[6]); @age4 = split(/ /, $vote_data[7]); # Add the first column titles to the rows of color votes unshift(@age1, "10-25"); unshift(@age2, "26-40"); unshift(@age3, "41-60"); unshift(@age4, "Over 60"); # Create the column titles in HTML by giving their address to the th # function and storing the return value in the @rows array @rows = th(\@col_titles); # Now create the data rows with the td function # and add them to the row addresses array push(@rows, td(\@age1), td(\@age2), td(\@age3), td(\@age4)); # Create the table for the female survey results # The address of the array of row addresses is passed to Trsults print "<br/><br/>"; print table({-border=>undef}, caption("<h3>Survey Data for Males</h3>"), Tr(\@rows) ); print end_html();