Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Skill 4
Using Perl and CGI in the Real World

  Counting your visitors
  How Perl deals with files
  Bringing your counter to the Web
  Running the counter

You are fairly well grounded by now in the processes that allow CGI to work with the World Wide Web. You can create a Perl program that draws its own Web page. You are also familiar with the methods used in CGI to pass information between the Web server and a CGI program.

But we haven’t done anything really useful yet. In this skill you’ll dive right in, take what you’ve learned thus far, and put it to work by creating an access counter for your Web site.

The Task: Counting Your Visitors

A popular feature among Webmasters is a simple access counter that keeps track of every visit to the Web site and puts up a little message at the bottom of the home page that displays for the user what number visitor they are, such as: “You are visitor No. 10,001!” See Figure 4.1 for an example of an access counter display.


Figure 4.1:  An example of one of the many access counters on the Web

Such a little thing should be fairly easy to lash together, shouldn’t it? Well, yes and no. It is a straightforward task, but it can’t be accomplished with HTML alone. You need the power of Perl in the background to do the work for you.

How to Go About It?

Creating an access counter is a programming problem, a system problem—and the best way to go about solving it is to first get up and walk away from your computer. That’s right; turn the thing off if you need to. You’ll be designing a system, a small and simple one, but a system nevertheless. The computer and the code are the last steps in the process of analyzing, designing, and implementing the solution to a problem.


NOTE:  In technospeak, a “system” really is nothing but the solution to a problem: It’s the analysis, design, and implementation of a tool or set of tools that will do the “thing” you want to do. The “thing” might be as monumental as keeping track of the daily transactions in a stock exchange, or it might be as simple as keeping track of a Web site’s visitors. No matter. The beginning steps are the same regardless of the complexity of the ultimate solution.

Think, Don’t Code!

Many programmers, especially beginners, just jump right in and start coding, and refine the bugs as they go along.

The problem with this approach is that it ties you inexorably to the computer and the programming tools you have at your disposal. As a result, you will end up with a solution that is entirely dependent on these tools.

It is more logical to approach a problem from a more general perspective. You have something you want to do, so how do you go about it? What, in general, do you need to do to implement a solution? We can distill this “thinking” phase down to three categories:

  Analysis
  Design
  Implementation

Once you have mapped out the steps you must take to solve the problem, once you have identified a general approach to a solution, then you can rummage through your kit of available tools and identify the ones you’ll need to solve the problem with what you have at hand.

So to begin, find a nice, comfortable chair in a dimly lit corner of the den, take a pad and pencil with you, and begin sketching. You need to think, not code.

Analysis

The first step in your “sketching” process is analysis. To effectively accomplish your task (or solve your problem) you need to properly identify what needs to be done. For example (as in Figure 4.2), to count the hits on a Web page you need to:

  Store a number somewhere
  Be able to read the number
  Be able to increment it (add 1)
  Write it out to the Web page
  Store the number again


Figure 4.2:  The requirements of an access counter

Design

Once you have analyzed your problem, you need to start thinking about design. For instance, the number your access counter derives has to be stored in a “permanent” place, where you can get to it any time you need to, change it, and store it again. A file on disk is a logical candidate, because data will stay in files even when your computer is turned off, barring some catastrophe. A disk file is the first choice of most programmers for storing data gathered by a program because it’s easy and relatively safe. So, you decide you’ll store the number in a file.

Having made that decision, we can flesh out the list of requirements from the analysis of the problem. The design of your solution, which is illustrated in Figure 4.3, can proceed step by step:

1.  If this is the first time the program runs, create a file, and store the value 1 in it. Proceed to Step 3.
2.  Otherwise, open and read the file. Add 1 to the value you have read.
3.  Display the new value on the Web page.
4.  Write the new value to the file.
5.  Close the file.


Figure 4.3:  Opening, reading, writing, and closing the counter file

Implementation

You will implement your solution using Perl, of course, and HTML. However, the requirements of the Web page access counter highlight one concept you haven’t covered yet: files.

How Perl Deals with Files

You have analyzed and designed a solution for the task of putting an access counter on your Web page. But the implementation step, according to the design, needs a method to store the counter in a disk file.

A disk is nothing more than a magnetic platter, very similar to a recording tape, that is capable of storing data. The low-level details of how that is accomplished aren’t particularly relevant to a discussion of files.

What is relevant is the idea that a file is nothing more than a collection of characters. They may not be characters in forms or combinations that are meaningful to humans, as in the case of a program file. But the characters are stored (recorded) in their original sequence on the disk and that’s how you deal with them.

The concept of a “file” on disk was intended originally to make an analogy to paper files in a cabinet full of folders, though the “folder” concept is only now becoming widespread in computer software. When you want to read a file, you have to “open” it first. When you’re done with it, you have to “close” it.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.