Creating a Multiple Choice Quiz System with CGI
Over the last few months, we have looked at a number of techniques that CGI programmers can use to work on their programs. This month, we will look at a multiple-choice quiz system that uses a combination of techniques to create a simple, but effective, system for creating quizzes for our users. By the end of this short project, you will have not only a good idea of how to implement this type of interaction, but a working four-question quiz, as well.
Before we can begin, we will need to decide on a file format which will contain the questions and answers for our quiz. We could put all of the questions and answers inside of the program itself, but moving them to one or more external files will let us reuse the software with other quizzes on our system. Given that this is a simple quiz, let's say that the questions and answers for each quiz are stored in a file whose name is the same as the quiz name. Thus the quiz named “presidents” will be stored in a file named “presidents”, while the quiz named “unix” is stored in a file named “unix”.
Now that we have decided on filenames, we need to decide on a format for the contents of the file. Let's take the easy route, and put one question and its associated possible answers on each line in the file, each separated by tabs, and ending with the letter “a”, “b”, “c” or “d”, that corresponds to the correct answer.
So that the file can contain comments and whitespace, we'll say that any line beginning with a hash mark (#) is considered a comment, to be ignored. The same goes for any line consisting solely of whitespace. Allowing for comments and whitespace makes it possible for us to comment out questions that we no longer want to use, without having to delete them altogether.
Here is a sample quiz on the subject of cranberries, which we will put in a file named, oddly enough, “cranberries”:
# This is the quiz file about cranberries.
# Comment lines contain a hash mark (#) in the # first column, and are ignored, as are lines # containing only whitespace.
What color are cranberries? Red White\
Blue Dark green A
What can you make with cranberries? Muffins\
Sauce Steak A and B D
Note that the questions and answers in this file can contain space characters, but not tab characters. This will typically not affect things very much, but it is a consideration to keep in mind. Also, while each line can be as long as needed, the question and its associated answers must remain on a single line of text (that is, must end in a carriage return).
Our quiz program actually consists of two different programs working in concert with each other. The first, askquestion.pl, produces an HTML form that presents the user with a question and a list of possible answers. That form will be submitted to another CGI program, checkanswer.pl, which determines whether the user has selected the correct answer.
Because both of these CGI programs will have to access the same quiz file, it is probably a good idea to centralize such functions in a single Perl 5 object. Such an object would have to read the file and return a question of our choosing from the list of available questions. To make things a bit more interesting, this object should include a method that retrieves a random question from the file, which makes the quiz less predictable for the user.
The object that we will use in our quiz program is shown in Listing 1. All this code means is that you can place a:
use QuizQuestions;
statement near the top of both CGI programs to create a Perl object that reads the questions to the “cranberries” quiz. To do this, you can use this statement:
my $quiz = new QuizQuestions("cranberries");
For example, you could retrieve the fifth question with:
my @question = $quiz->getQuestion(5);or a random question with:
my @question = $quiz->getRandomQuestion;As you can see, the QuizQuestion object in Listing 1 has nothing to do with CGI programming per se. Even if we were creating a quiz system that wouldn't be used on the Web, this object would be a good starting point. By using an object to represent our data, we have also made it possible to change the file format we are using without modifying the CGI programs that access the data. If we were so inclined, we would be able to move the quiz data into an SQL table, and access it via a database client from within Perl. As long as the interface to the outside world remains the same, our CGI programs wouldn't care.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Validate an E-Mail Address with PHP, the Right Way
- New Products
- Tech Tip: Really Simple HTTP Server with Python
- Developer Poll
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




51 min 58 sec ago
1 hour 14 min ago
1 hour 24 min ago
1 hour 28 min ago
1 hour 58 min ago
4 hours 50 min ago
5 hours 25 min ago
5 hours 26 min ago
5 hours 27 min ago
5 hours 29 min ago