Controlling a Multistage CGI Script

The next interesting section of the script is a long chain of unless-elsif blocks that test the contents of the $action variable, and execute various subroutines based on what’s in there. The idea here is to have a single CGI script that will carry out a whole sequence of actions as a user clicks his way through multiple dynamically generated pages, with the passed action parameter controlling which part of the script is run for each invocation:

unless ($action) {
    &show_links;
} elsif ($action eq 'show_student_form') {
    &show_student_form;
} elsif ($action eq 'show_leader_form') {
    &show_leader_form;
} elsif ($action eq 'Create student page') {
    &post_student_page;
} elsif ($action eq 'Create leader page') {
    &post_leader_page;
} elsif ($action eq 'regenerate_site_links') {
    &regenerate_site_links;
} else {
    $err_msg = "<LI>Wacky 'action' param '$action'. Shouldn't be able 
               to get here.";
}

I’ve had people tell me that my use of unless-elsif is evil, but it makes sense to me when I read it out loud. Your mileage may vary.

The overall gist of this script is that each unless-elsif block will load up the $content variable with a different page to be delivered back to the requesting user. If anything goes wrong, a descriptive error message will be stuck in the $err_msg variable. After all the unless-elsif stuff is done, the script will check for the presence of anything in $err_msg, and if something is there, it will create an HTML error message incorporating ...

Get Perl for Web Site Management now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.