Perl Beginners - Addressbook Tutorial Introduction

Table of Contents | Step 1

Motivation

I thought the inception of the beginners-cgi mailing list was a good idea. I have been programming Perl for 5 years now, and knew from experience the wrath that could be inflicted if a person posted to one of the comp.lang.perl.* newsgroups without carefully considering every word in their message. Even if someone posted a solution to a problem, chances were they'd get ripped up by other more experienced programmers on a 'better' or 'more elegant' way to do it. That kind of consequence can be a little intimidating to the newbie wanting to know why their script doesn't work. RTFM is a common answer to lots of newbie questions in the perl newsgroups.

So, a mailing list where anything goes without fear of being flamed is a good thing, imho. After spending several days reading messages from people with code snippets asking for help, I realized how far I had come since writing my first perl script many years ago. And, I saw there were others struggling with the same problems I had when I first started. I decided to work on a complete tutorial to demonstrate how I develop a project from initial concept to production. What follows is my humble attempt to give back to the Perl community.

Rules

Well, let's call them guidelines instead of rules. Here are the guidelines I use when developing a project:

  • the code should be flexible
  • the code should be easy for someone else to edit
  • the Perl code should be separate from the HTML
  • for readability's sake, all Perl code should fit on one page (this one is occasionally broken)
  • Assumptions

    I will assume that readers of this tutorial will:

  • have a basic understanding of how to write a Perl program
  • know how to download and install modules from CPAN
  • have a basic understanding of how to write SQL
  • have root access to the box they're developing on
  • Ok, maybe the last assumption is a bit too much. It means that if you are using a box owned by your ISP where you have access only to your own cgi-bin, you may have to put all of your modules and handlers in the cgi-bin. If you do this, be sure to limit access to the directories containing your Perl code or your modules may be served up as plain text by the Web server. Not a good thing.

    Modules

    All of my code use the following modules, which are available from CPAN:

  • Carp
  • DBI
  • CGI
  • HTML::Template
  • Class::MethodMaker
  • Data::Dumper
  • If you have never used these modules, or are unfamiliar with some of them, don't worry. As I go along with the tutorial, I'll give a basic explanation of how they work and why I use them. However, that should not stop you from getting them from CPAN, installing them, and reading the documentation.

    Server Configuration

    I run RedHat Linux/6.2, Apache/1.3.14, mod_perl/1.24_01, and PostgreSQL/7.0.3-2. Since MySQL is pretty prevalent in the Perl community, I'll try my best to point out the differences between PostgreSQL and MySQL and how to change any SQL so it will work with MySQL. For the most part, I'll avoid mod_perl, since not everyone runs Apache.

    Why I use HTML::Template

    Every place I've ever worked, the programming staff is divided into two groups: those who know how to program in Perl, and those who don't. Usually, the latter is tasked with developing the HTML and, in almost all cases, develop, tweak, or somehow change the HTML that is displayed to the user. Using HTML::Template provides a way for the HTML developer to massage the look-and-feel of the output without touching the code that generates it. All my code relies heavily on it, so if you're unfamiliar with HTML::Template, I'd suggest installing it and reading the documentation. You can do that by typing perldoc HTML::Template.

    Legalspeak

    Unfortunately, I have to include the following:

    Copyright © 2001 by Peace Computer Systems

    1. Disclaimer/Warranty THE SITE AND THE MATERIALS CONTAINED ON THE SITE ARE PROVIDED BY PEACECOMPUTERS.COM ON AN AS IS BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO THE OPERATION OF THE SITE, THE INFORMATION, OR CONTENT INCLUDED ON THE SITE. TO THE FULLEST EXTENT POSSIBLE BY APPLICABLE LAW, PEACECOMPUTERS.COM DISCLAIMS ALL WARRANTIES, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
    2. Limitation of Liability PEACECOMPUTERS.COM WILL NOT UNDER ANY CIRCUMSTANCES BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR PROFIT, ARISING OUT OF THE USE, OR THE INABILITY TO USE THE SITE. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF INCIDENTAL PUNITIVE OR CONSEQUENTIAL DAMAGES, SO THE ABOVE LIMITATION OR EXCLUSION MAY NOT APPLY TO YOU. YOUR USE AND BROWSING OF THE SITE IS AT YOUR OWN RISK.
    3. Copyright Permission to use, copy, modify, and distribute this software and its documentation for any purpose is hereby granted without fee, provided that (i) the above copyright notices and this permission notice appear in all copies of the software and related documentation, and (ii) the names of fliptop and Peace Computer Systems may not be used in any advertising or publicity relating to the software without the specific, prior written permission of fliptop and Peace Computer Systems.

    Coming Next: Step 1 - Analyzing the project


    Copyright © 2001 by Peace Computer Systems