Question and answer system for the web
Quiz Master
Catalyst is the Ruby on Rails of the Perl universe. When you are developing a web application like a quiz, using the MVC framework is really convenient and helps keep the underlying components cleanly separated.
Whether guessthelogo.com asks you to pick the right company logo out of strikingly similar variations, or Food.aol.com invites visitors to identify candy bars by their cross sections (Figure 1), an entertaining quiz is always welcome during a hard day at work. You can expect your colleagues to forward the URLs, and comparing scores later on will mix up the hacking order and trigger fascinating discussions.
Are you interested in compiling your own quiz? Figure 2 shows our home-made quiz at work. To make the code reusable, the application retrieves the questions and multiple choice answers from a YAML file (Figure 3). The example uses a selection of questions from the USA immigration test. For example, applicants need to know how many stars are displayed on the US flag and what they symbolize [4].
The web application parses the YAML file and displays each question individually on a new page. Whereas the YAML file always lists the correct answer first, the application will display possible choices in random order to keep things interesting.
The implementation is not particularly sophisticated, but there are quite a few things to think about: nicely designed HTML with dynamically managed fields, session management between the individual questions to prevent the application from forgetting the user's score, and a results page that tells the user the final score and invites them to try the next round (Figure 4). Finally, the server should never trust the client, because the client just might cheat.
Catalyst Framework
The Catalyst Framework [5] helps Perl programmers with projects of this kind by automatically creating a program code skeleton to which the developer simply adds the application-specific components.
The fact that the system gets split up into the model (data representation), view (HTML display), and controller (flow control), has proven to be very effective in web application development, as it supports clear code separation and thus easier maintenance.
Installing the Framework
The Catalyst modules are available from CPAN. Because of their sheer number, I recommend downloading a prebuilt package. On a Debian-based system, the command line
sudo apt-get install libcatalyst-perl libcatalyst-modules-perl
installs all the modules and a bunch of dependencies. To avoid having to start from scratch, call catalyst.pl QuizShow from the command line and Catalyst creates a new QuizShow directory for the newly created application. It drops about 30 files into various subdirectories to let you run the whole enchilada straight away. Among other things, this includes a Makefile.PL file, to package the application CPAN-style, predefined configuration files, module skeletons to fill in application-specific code, and various scripts to create new parts and launch the application in different ways.
Later, you can run it as a CGI script or using Mod_perl on an Apache server. During development, you might like to launch the web server included with the distribution:
cd QuizShow script/quizshow_server.pl
This immediately launches the server as shown in Figure 5 and outputs nicely formatted information on the server configuration and the URL at which the browser can reach it.
The default setting is http://localhost:3000. If you enter it in a browser, you get to see the Catalyst welcome page. Production systems will later use an Apache server instead.
Http with a Memory
When a browser communicates with a web server, neither of them saves state between individual requests, unless session cookies and server-side session files take care of it explicitly. A quiz that forgets the score between questions wouldn't be all that useful.
Session management is boilerplate logic, and it's easy to get wrong, so Catalyst offers a turn-key solution, again as a Debian package.
The following command line installs the required Perl modules:
sudo apt-get install libcatalyst-plugin-session-fastmmap-perl
To make sure the quiz automatically feeds a session cookie to the browser on first contact, besides allocating a cache server-side and storing user data in that space, you need to change the code
use Catalyst qw/-Debug ConfigLoader Static::Simple/;
in the automatically generated lib/QuizShow.pm file to:
use Catalyst qw/-Debug ConfigLoader Static::Simple Session Session::State::Cookie Session::Store::FastMmap/;
This allows the application to access a Perl hash with session information by simply calling the Catalyst context object's session() method. Catalyst stores this data automatically under the session ID of the Catalyst browser cookie and manages it on the server without requiring any development effort.
Of course, this approach will only work if the browser talks to the same server for each new request, and not to an arbitrary member of a server farm. Catalyst offers database-based sessions for more complex configurations to handle this.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.