Listing 3. The Updated Version of new and Method loadFile
for QuizQuestions.
sub new
{
# Get our arguments
my $type = shift;
my ($quizName) = @_;
my $self = {};
# Set instance variable containing
# quiz name
my $self->{"quizname"} = $quizName;
# Set instance variable containing questions
my @questions = ();
my $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 file for $self->{"$quizname"} ";
# Loop through the question file
while (<QUESTIONS>)
{
$line_number++;
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++] = $_;
}
close(QUESTIONS);
# Now turn @questions into an
# instance variable
$self->{"questions"} = \@questions;
# If there weren't problems,
# then return 0
return 0;
}
Copyright © 1994 - 2018 Linux Journal. All rights reserved.