Listing 3: QuizQuestions.pm, with a slight bug fix.

#!/usr/bin/perl -w

package QuizQuestions;

use strict;
use diagnostics;

# Get the CGI-enhanced version of the Carp debugging module, available
# from CPAN, at http://www.perl.com/CPAN
use CGI::Carp;

# All of the questions should be in the same directory
my $questionDir = "./";

# ------------------------------------------------------------
sub new
{
    # Get our arguments
    my $type = shift;
    my ($quizName) = @_;
    my $self = {};

    # Set instance variable containing quiz name
    $self->{"quizname"} = $quizName;

    # Set instance variable containing questions
    my @questions = ();
    $self->{"questions"} = \@questions;

    bless $self;
}

sub loadFile
{
    # Get ourselves
    my $self = shift;

    # Set some initial variables
    my @questions = ();
    my $counter = 0;

    # Open the questions file
    open (QUESTIONS, $questionDir . $self->{"quizname"}) || 
	return "Could not open " . $self->{"quizname"} . " for reading";

    # Loop through the question file
    while (<QUESTIONS>)
    {
	next if /^#/;      # Ignore comment lines
	next unless /\w/;  # Ignore whitespace lines

	chomp;

        # Split the line across tabs
        my @list = split(/\t/);

        # Check to make sure that there are six fields
        if ($#list != 5)
        {
            # Return the line containing the error
            return "Error in line: \"$_\"";
        }
        else
        {
	    # Add this question to our list
	    $questions[$counter++] = $_;
        }

    } # This brace used to be in the wrong place

    close(QUESTIONS);

    # Now turn @questions into an instance variable
    $self->{"questions"} = \@questions;

    # If there weren't any problems, then return 0
    return 0;
}

# ------------------------------------------------------------
sub getQuestion
{
    # Get information about ourself
    my $self = shift;

    # Get the argument, which should be an integer
    my($index) = shift;

    # Get the question corresponding to that number
    my $question = @{$self->{"questions"}}[$index];

    # Turn that into a list
    my @question = split(/\t/, $question);

    # Add the number of the question to the list
    push(@question, $index);

    # Return the list to the caller
    return @question;
}

# ------------------------------------------------------------
sub getRandomQuestion
{
    # Get information about ourself
    my $self = shift;

    my @questions = @{$self->{"questions"}};

    # Seed the random number generator
    srand(time);

    # Choose a random number from among the questions
    my $random_number = int(rand($#questions + 1)) + 0;

    # Now invoke getQuestion with our random number
    return ($self->getQuestion($random_number));
}

# ------------------------------------------------------------

sub addQuestion
{
    # Get ourselves
    my $self = shift;

    # Get our arguments
    my ($question, $a1, $a2, $a3, $a4, $correct) = @_;

    # Turn our arguments into a string
    my $new_question = join("\t", @_);

    # Get our instance variable
    my @questions = @{$self->{"questions"}};

    # Add the new question
    push (@questions, $new_question);

    # Reset the instance variable
    $self->{"questions"} = \@questions;

    # Return successfully (= 0)
    return 0;
}

# ------------------------------------------------------------
sub saveFile
{
    # Get ourselves
    my $self = shift;

    # Open the questions file for writing
    open (QUESTIONS, ">$questionDir" . $self->{"quizname"}) || 
	return "Could not open " . $self->{"quizname"} . " for writing";

    # Loop through the questions
    my @questions = @{$self->{"questions"}};

    my $question;
    foreach $question (@questions)
    {
	print QUESTIONS $question, "\n";
    }

    close(QUESTIONS);

    return 0;
}




# Indicate that the module was loaded successfully
1;
