Perl Beginners - Addressbook Tutorial Step 3 - Creating perl classes and our first cgi |
Overview |
First, let's go over some locations for our code. |
As I stated in the Introduction, I run RedHat Linux and Apache. Therefore, the directories I use will be specific to that type of configuration. Solaris and Windows users may have to make some adjustments. |
My Perl handlers and utility classes will be stored in |
|
and the base class will be stored in |
|
The server root will contain these directories: |
|
Obviously, all my cgi scripts will be in the cgi-bin directory, and I'll use the templates directory to store the HTML::Template files. Plain HTML files are stored in the html directory. |
The base class |
The base class contains any variables and methods that are common to all the other classes for this project. So let's ask ourselves what each cgi request will need: Later, we'll include the root directory where our uploaded images will be stored. |
Here is a base class to start off with, AddressBook.pm: |
|
Let's look at each part of this class. |
|
Each class will begin with the package declaration, creating a namespace where the the package's variables, methods, etc. will reside. require 5.6.0 and use strict are two pragmas that say, "This code is meant for Perl version 5.6.0 or higher, and I want to eliminate sloppy constructs." use strict will force us to use my or our when declaring variables. no warnings shuts up "Variable $x used only once.." warnings. |
|
Although not necessary, declaring a $VERSION variable can be useful. For example, try typing this at a command prompt: |
|
Without the $VERSION declaration, it will print a blank line. The $VERSION declaration also is useful if you're using CVS, which is beyond the scope of this tutorial. |
|
Here we specify the arguments necessary to get a handle to the database. Notice I am using DBI:Pg; MySQL users should use DBI:mysql. Use your own username and password to make the database connection. $DBI_OPTIONS is a reference to a hash of arguments that tell DBI how to behave. Read the documentation for DBI for more information on these arguments. You can do that by typing perldoc DBI. Using @DBI_CONNECT_ARGS makes DBI connect calls easier; more on that later. |
|
Here we specify where the HTML::Template files will be located. |
|
All classes must return TRUE, so we include the 1;. The token __END__ (or alternatively, a Control-D or Control-Z character) may be used to indicate the logical end of the script before the real end-of-file.1 We'll put our comments after it (later). |
Our first cgi |
According to the rules in the Introduction, the cgi should be flexible, easy to edit, separate from the html, and should fit on one page. Here's what we'll need the cgi to do: |
We'll need just one cgi to process all the non-restricted applications. Here's the code for handler.cgi: |
|
Let's examine this script one section at a time. |
|
The -w switch on the shebang line tells Perl to turn on warnings. We'll use warnings while the code is in development. Next we use the Perl handler (which we have not written yet), and then we use CGI. |
|
First we construct a reference to a new CGI object and read the action parameter value passed to the script (or set it to 'start' if omitted). Then we construct a reference to a new Handler object, passing in a hash with the action and the reference to the CGI object. |
|
Here we get a reference to a required parameters hash. We'll store the arguments to pass to the action method in @args. Then, for every key in the required parameters hash, we'll put the CGI parameter values passed in each form element name into an array. If the array contains just one value, we'll push the parameter key and value into @args. If the array contains more than one value, we'll push a parameter key and a value onto @args for every element in the array. Why go through all of this trouble? Let's assume we're executing an action method to search for a particular record by the record's ID. The URL may look like this: |
|
The @args array will contain the values 'record_id', '123'. But if the search method is capable of looking for more than one record at a time, the URL may be as such: |
|
In this case, the @args array will contain 'record_id', '123', 'record_id', '321', 'record_id', '213'. |
|
Finally, we call the action method, passing in the arguments. Then we print the obligatory Content-type header and call the Handler's sendoutput method. |
You may notice that when this cgi is pre-compiled (by typing perl -c handler.cgi) that you get an error stating Perl can't locate Handler.pm. That's because we haven't written it yet, which comes next. |
Coming Next: Step 4 - The Perl handler (and a table of contents for the tutorial) |
1 - Wall, Larry, et. al. (2000). Programming Perl, 3rd Edition. Sebastopol, CA: O'Reilly & Associates, Inc., p. 68. |
Copyright © 2001 by Peace Computer Systems |