LJ Archive

Listing 5. create-quizfile.pl, The Program That Uses QuizQuestions.pm

to Save a New Quiz File.
#!/usr/bin/perl -w

use strict;
use diagnostics;
use CGI;
use QuizQuestions;

# Create an instance of CGI
my $query = new CGI;

# Send an appropriate MIME header
print $query->header("text/html");

# Die if the user did not name a quizfile
&log_and_die("No quizfile named") unless
   ($query->param("quizfile"));

# Create a new instance of QuizQuestions
my $quiz = new QuizQuestions($query->param("quizfile"));

# Now iterate through each question that we
# received, creating a new question for each
# one

my $counter;
foreach $counter (1 .. 4)
{
    # Handle only as many questions as
    # were filled in by checking to see
    # if the question was entered last unless
      $query->param("question-$counter");

    # Set the question
    my @question =
   ($query->param("question-$counter"),
    $query->param("answer-a-$counter"),
    $query->param("answer-b-$counter"),
    $query->param("answer-c-$counter"),
    $query->param("answer-d-$counter"),
    $query->param("correct-$counter"));

    # Add the question to the quiz
    $quiz->addQuestion(@question);
}

# Save the file to disk
my $error = $quiz->saveFile;
&log_and_die($error) if $error;

# Now print a message for the user
print $query->start_html(-title => "Done!");
print "<P>The quizfile was updated.</P>\n";
print $query->end_html;

exit;

sub log_and_die
{
    my $message = shift;

    # Send a message to our users
    print $query->start_html(-title => "Error!");
    print "<P>$message</P>\n";
    print $query->end-html;

    # Send an appropriate message to the HTTP
    # error log
    die $message;
}
LJ Archive