Lab 10 - Running on the Web

Overview
Today, we review the basics of writing CGI programs in Perl. This will be directly applicable to the project (which you should be working on!)

 

Getting Started
First, download the database we'll use for this lab: rest.db. You can either right-click this link, choose "Save As", and then upload the saved file to Sulu using SSH; alternately, you can use the 'wget' program as follows:
wget http://sulu.informatics.indiana.edu/fil/lab10/rest.db

This database is a hash of lists of hashes (yuck). The keys of the first hash are zip codes; each zip code is related to a list of hash references. Each of these hash references refers to a hash of restaurant information just like the one we dealt with in Project Checkpoint 2.

The first step - before writing the real CGI program - is to write a simpler program to get the broad strokes.

Do this:
Write a script called restaurants.pl that, given a zip code on the command line, uses the rest.db database to print out the titles of the restaurants in that zip code. An example output follows. Put all the code related to printing restaurants in a subroutine called print_restaurants that takes a zip code and a database file name as arguments.

lunar lab10/soln % ./restaurants.pl 47401
Applebee's Neighborhood Grill
Arby's
Arby's
Baldy's Pizza
Bear's Place
Bloomington Bagel Co
Bob Evans Farms Restaurant
Brave New Deli
Bucceto's Pizza & Pasta Restaurant
Howies Bagel Bakery
Lennie's and the Bloomington Brewing Co.
Mikado Japanese Restaurant
Mother Bear's Pizza

 

Moving into the internet age

Now time to make our program into a CGI program. There are several ways to do this, but let's choose the easiest. This is to adapt the program from lecture not long ago; suppose we call it restaurant.cgi:

#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print header, start_html('Restaurant form');
print h2('Find restaurants in your area');
print start_form;
print p('Please enter ZIP code: ', text_field('zip'), submit);
print end_form;

if (param()) {
    print_restaurants(param('zip'), 'rest.db');
}

print end_html;

The above can be most of the main method for your program. Note that it prints out a bunch of boilerplate HTML stuff (header, start_html, and the like) and then a zip code form. If it finds parameters, it knows that this is the second time it has been called (i.e., it has been given a zip code by a previous invocation of itself), so it calls the print_restaurants function.

Do this:
Create the restaurant.cgi program. Most of the code is given for you above; however, you'll need to make at least two modifications:
  • Write the print_restaurants method by adapting the method you wrote for the first part of this assignment. Use an "unordered list" (ul) to print the restaurants; for details on how to do this, see the CGI.pm documentation.
  • Modify the code you were given above so that it calls print_restaurants in either of the following two cases:
    • @ARGV > 0
    • param() > 0 (already there)
    That way, you can debug your program from the command line.

 

Testing your program with a web browser
Now it's time to test your newly-created program with a web browser. As in the first lab, copy all files required (your CGI script and your database) to /var/www/cgi-bin/YOURUSERNAME. Then cd to that directory and execute chmod 755 on each file, to make sure they can be accessed from the web. Finally, open your web browser and point it to http://sulu.informatics.indiana.edu/cgi-bin/YOURUSERNAME/restaurant.cgi to make sure everything works. Please name your script exactly restaurant.cgi and leave it there after the lab is over, as we might use it during grading.

 

Extra Credit

For 2 points of extra credit, add an scrolling list to your form with the names of all the possible attributes in it. Allow the user to select which attributes are to be printed. Then let a zip code query print out a bulleted list of restaurants, where each restaurant is a bulleted list of attributes, like this:

  • Bob Evans
    • City: Bloomington
    • State: IN
    • Zip: 47405
  • Applebee's Neighborhood Grill
    • City: Bloomington
    • State: IN
    • Zip: 47405

 

Turning everything in

That's it! Time to tar the lab10 folder as usual and upload it to the Lab 10 assignment on Oncourse. Make sure that all files are included in the submission. We will take off points for incorrect submissions, even if you really finished the assignment on time.

Have a good weekend!

Thanks to Jacob Ratkiewicz for lab content, Sid Stamm for CSS stylesheets, and Rob Signorelli for original syntax-highlighting JavaScript.