CGI.pm Training

Books and Other Resources

Unfortunately, there are no cgi.pm books of excellent quality. All of them are mediocre.

  1. Perl in a Nutshell
    This is a standard text book of Perry's Adv. Perl class. If you haven't noticed already , there's a handy little reference section on CGI.pm. This is not really a CGI book, but I'm just listing it here because most of us already have this book.
  2. "Official Guide to Programming with CGI.pm : The Standard for Building Web Scripts"
    Author: Lincoln Stein
    A tutorial style book. Used as the textbook in Perry's Perl CGI class. The code in the book is confusing and suffers from an overly brilliant coding style. This is the only book that describes every single function of the API. The other books only document the functions for generating form widgets. Unfortunately, Lincoln Stein's reference section is badly organized.
  3. CGI Programming with Perl
    An O'Reilly book
    Perry hates this book, but I like it. It's a book you would read after you mastered the basics of CGI.pm. It covers many advanced topics that you wouldn't necessarily show to a beginner.
  4. John Perry's CGI scripts
    He's the guy that teaches Perl CGI. He has loads of example CGI scripts on his site. Very instructive for the novice CGI programmer.

Homework

  1. Running a Perry example

    Our first task will be to run a CGI script.

    It is advised that in order to broaden your understanding of

    1. If you haven't done so already, install Linux with Apache
    2. Locate a file called httpd.conf. On Redhat Linux, the location is /etc/httpd/conf. On other distributions, the location is /usr/local/apache/conf. If it's in neither, you'll need to execute find to discover the location.
    3. Place this directive in httpd.conf ScriptAlias /~loginname/cgi-bin/ /home/loginname/public_html/cgi-bin/
    4. Go to Perry's CGI example. This is a short little CGI script. Just copy and paste it into your text editor and save it in your cgi-bin directory.
    5. chmod 755 *.cgi
    6. Now open the cgi file from your web browser. The url should look something like this
      localhost.localdomain/~loginname/cgi-bin/filename.cgi
      or
      127.0.0.1/~loginname/cgi-bin/filename.cgi
    7. Click a few checkboxes, and hit the set button. Notice how the next page is dynamically generated.

    CGI scripts are invoked multiple times to generate different pages. The first invocation occurs when your browser first visits the cgi script. During this first invocation, subroutine param() returns null. By testing for a null param(), the CGI script builds the 1st page. When you click the submit button, labelled 'Set' in this example, the CGI script will be invoked once again. In this 2nd invocation param() will actually return something. param() returns the values of form widgets from the last invocation. Calling param() by itself, gives you a list of names of widgets of the last invocation. If you call param() with a name of a widget, you get back the value of the widget. Be aware that param values only exist for one invocation. If you want param values to stay around, you'll need to add hidden widgets to the generated page to hold those values.

  2. Creating your first CGI script

    CGI.pm contains many functions that generate HTML for you. A bare basic script will look like this

              print header;
              print start_html;
                 ...
              print end_html;
            

    In the CGI.pm every single HTML tag can be generated by the appropiate function call. These are only documented in their entirety by the Lincoln Stein book. Most other books just document the API for the form widgets.

    In addition to using CGI.pm functions to generate HTML, you are allowed to print raw HTML,e.g.

              print q   # q {} is the same thing as single quoting
              {
                <table>
                  <tr>
                    <th>header</th>
                    <td>data</th>
                  </tr>
                </table>
              }
            
    1. Download Emcee's cgi.zip file, and open thai_chickent_burrito.cgi. This cgi script generates only one page, and that's why there's no checking of param. Thai Chicken Burrito is the bare minimum required for a food item page. This will be your template for the next step.
    2. Open www.waiter.com, and look up some food items. Create your own food item based on a food item from a restaurant. Be aware that restaurants span a range of complexities. Una Mas has some of the simplest food items. Amici's is medium complexity. Le Boulanger is quite difficult.
    3. Now point your web browser to your cgi file and run it. You might have some errors in your cgi file, so keep in the mind the following hints.

      Hint: Syntax errors in your cgi script can be ferreted out if you do

      perl -c filename.cgi

      Hint2: use view->source from your browser to look at the generated source

    4. You have the option of adding JavaScript alongside your CGI file. In start_html, add this parameter
                    -script => q   # q{} is the same thing as single quoting
                      {
                        // Your JavaScript here
                      }
                  
  3. Collating your independent food item page

    Multi page cgi scripts typically have a format that looks like something like this

              my $go = param ('go');
              if (!param || $go eq 'start_over')
              {
                start_page();
              }
              elsif ($go eq 'first')
              {
                first_page();
              }
              elsif ($go eq 'cart')
              {
                shopping_cart();
              }
              else
              {
                die "Unexpected page request\n";
              }
            
    1. Now that you have an independent food item, you must add in your food item into a restaurant cgi file, e.g. una_mas.cgi, le_boulanger.cgi. Start off by putting your food item in a subroutine.
    2. Next, add a call to your subroutine in the main if-else.
    3. Oh, and don't forget to add your food item to the menu. How would the user find the food unless it was listed in a menu? When constructing your link use the following format.

      <a href="restaurant.cgi?go=food_name">

      That thing with the go embeds the go value into the URL for the next invocation of the CGI script.
contact: emcee.lam@juno.com
last modification: