return to first page linux journal archive
keywordscontents

Listing 2. askquestion.pl

#!/usr/bin/perl -w 

# Turn on strict error checking
use strict;
use diagnostics;

# CGI.pm is available from CPAN, 
# at http://www.perl.com/CPAN
use CGI;  

# Include our object
use QuizQuestions;

# Where should we send answers?
my $answer_cgi = "checkanswer.pl";

# ---------------------------
# Create a new CGI object, and output an initial
# MIME header
my $query = new CGI;
print $query->header("text/html");

# Get the quiz name
my $quiz_name = $query->param("keywords");

# Create a new QuizQuestions object, using
# the name of our quiz
my $quiz = new QuizQuestions($quiz_name);

# Now choose a random question
my ($question, $answerA, $answerB, $answerC, 
    $answerD, $rightAnswer, $questionNumber) = 
    $quiz->getRandomQuestion;

# Produce the appropriate HTML
print $query->start_html(-title => "Quiz"), "\n";

print "<P>$question</P>\n";

print "<Form method=\"POST\"
action=\"$answer_cgi?$quiz_name\">\n";

print "<P>A: $answerA<input type=\"radio\"
name=\"answer\" value=\"a\"></P>\n";
print "<P>B: $answerB<input type=\"radio\"
name=\"answer\" value=\"b\"></P>\n";
print "<P>C: $answerC<input type=\"radio\"
name=\"answer\" value=\"c\"></P>\n";
print "<P>D: $answerD<input type=\"radio\"
name=\"answer\" value=\"d\"></P>\n";

print "<P><input type=\"hidden\" name=\"questionNumber\" ";
print "value=\"$questionNumber\">\n";

print "<P><input type=\"submit\" value=\"Check
answer\"></P>\n";
print "<P><input type=\"reset\" value=\"Reset
selection\"></P>\n";

print "</Form>\n";

print $query->end_html;