jdlev has asked for the wisdom of the Perl Monks concerning the following question:

I am getting some CGI errors when I try to run this code. The code I found is straight out of a book, as I'm trying to learn how to run a poll on my website.

To see the CGI errors, please check them out here: http://www.forexballot.com/groundhog.pl

Here is my code:

my %groundhog_map = # hash that maps groundhog names to labels ( "jimmy" => "Jimmy the groundhog (Sun Prairie, Wisconsin)", "phil" => "Punxsutawney Phil (Punxsutawney, Pennsylvania)" ); print header (), start_html (-title => "Vote for Your Favorite Groundhog", -bgcolor => "white"); # Dispatch to the proper action based on user selection my $choice = lc (param ("choice")); #get choice, lowercased if ($choice eq "") #initial script invocation { display_poll_form(\%groundhog_map); } elsif ($choice eq "submit") #tally vote, show current results { process_vote (\%groundhog_map, 1, param ("name")); } elsif ($choice eq "results") #just show current results { process_vote (\%groundhog_map, 0); } else { print p (escapeHTML ("Logic error, unknown choice: $choice")); } print end_html(); sub display_poll_form { my $map_ref = shift; #groundhog name/label map print start_form (-action => url()), p ("Which groundhog is your favorite?"), #pose question radio_group (-name => "name", -values => [ sort (keys (%{$map_ref})) ], -default => "[NO DEFAULT]", -override => 1, -labels => $map_ref, -linebreak => 1), #display buttons vertically br(), submit (-name => "choice", -value => "Submit"), end_form (); #add link allowing user to see current results without voting print hr(), a ({-href => url() . "?choice=results"}, "See current resu +lts"); } CREATE TABLE groundhog ( name CHAR(10) NOT NULL, /* groundhog name */ tally INT UNSIGNED NOT NULL DEFAULT 0 /* number of votes */ ) INSERT INTO groundhog (name) VALUES ('jimmy'), ('phil') sub process_vote { my ($map_ref, $tally_vote, $name = @_; my ($dbh, $rs_ref, $row_ref, $sum, @table_row); $dbh = WebDB::connect (); if ($tally_vote) { #make sure name was given and that it's one of the legal names if (defined ($name) && defined ($map_ref->{$name})) { $dbh->do ("UPDATE groundhog SET tally = tally + 1 WHERE na +me = ?", undef, $name); print p ("Thank you for voting!"); } else { print p ("No vote was cast; did you make a choice?"); } } print p (" The current results are: "); #retrieve result set as a reference to a matrix of names and tallies $rs_ref = $dbh->selectall_arrayref ("SELECT name, tally FROM groundhog +"); $dbh->disconnect (); #compute sum of vote tallies $sum = 0; map { $sum += $_->[1] } @{$rs_ref}; if ($sum == 0) #no results! { print p ("No votes have been cast yet"); return; } #Construct table of results: header line first, then contents. #For each groundhog, show votes as a tally and as a percentage #of the total number of votes. Right-justify numeric values. push (@table_row, Tr (th ("Groundhog"), th ("Votes"), th ("Percent"))) +; foreach $row_ref (@{$rs_ref}) { my $label = $map_ref->{$row_ref->[0]}; #map name to descriptive l +abel my $percent = sprintf ("%d%%", (100 * $row_ref->[1]) / $sum); push (@table_row, Tr ( td (escapeHTML ($label)), td ({-align => "right"}, $row_ref->[1]), # tally td ({-align => "right"}, $percent) # % of total )); } print table (@table_row); }
I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: Trouble with poll code
by moritz (Cardinal) on Aug 13, 2009 at 15:51 UTC
    You can't simply insert SQL into a perl script and hope that it will work by some magic.

    Use DBI to interact with your database.

Re: Trouble with poll code
by ig (Vicar) on Aug 13, 2009 at 16:48 UTC
Re: Trouble with poll code
by Utilitarian (Vicar) on Aug 14, 2009 at 07:17 UTC
    The return page you refer to contains the following.
    Undefined subroutine &main::start_html called at D:\Web\Forex Ballot\g +roundhog.pl line 10.
    In order to use the functions of a module you have to use the module. So previous advice from moritz to use DBI (and the appropriate DBD) applies but you also need to use the CGI module, and as you are using it in a non-OO fashion, you need to use it in the standard form, and you may need to debug your html so you should print it in a way you can read. Thus:
    use CGI::Pretty qw(:standard);
    Read through the book again looking at all the words ;)