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


Consolidating the Code

The remainder of the program prints out standard HTML statements that put a title at the top of the page and write an H1 heading in its body. Pretty simple stuff for anyone who’s ever done any HTML programming. The exciting part of all this is that the HTML code was generated from a program. It wasn’t just typed into a text editor and executed by a Web browser. You created it on the fly.


NOTE:  Perl scripts actually generate HTML code on the fly, instead of just formatting and printing a list of code as in traditional HTML. This will come in handy when your Web pages become more interactive.

You’ll learn in Skill 3 how you can use the capability to generate HTML on the fly to actually make decisions on what kind of page you will draw, based on the information that is sent to your Perl program.

For now, let’s introduce a couple of Perl concepts that will make subsequent projects a little easier: the subroutine and the require function.

Perl Subroutines

Perl’s inventor, Larry Wall, has written that “the three great virtues of a programmer [are] laziness, impatience, and hubris.” He also wrote that learning Perl will increase the value of your resume.

Both statements express sublime truths, though, like Zen, the concepts take some getting used to. Before we begin to explore the concept of subroutines in Perl, let’s examine the first statement.

Laziness and impatience lead good programmers to absolutely kill themselves to avoid doing things more than once. This is why the subroutine was invented—to keep programmers from having to do the same thing over and over again. A subroutine is a block of code that can be called from another block of code. The latter block jumps into the subroutine, it is executed, and then control passes back to the block that called the subroutine. See Figure 2.7 for an illustration of this process.


Figure 2.7:  How a Perl subroutine works

Webster defines hubris as “wanton arrogance arising from overbearing pride or from passion.” After you’ve been programming for a while, you’ll know what they mean.

In beginner’s terms, the rules of programming are a lot simpler: Anything, absolutely anything, that you find your program doing more than once should be put in a subroutine.

Using the require Command

The hellowww.pl program doesn’t do much of anything more than once. However, it contains some code that, once you refine it in later skills, will be used at least once in every CGI program you write. These are the print statements that set up:

  The MIME header
  The HTML header
  The HTML ender

Let’s look at each of these. Here is the MIME and HTML header code:

   print ";Content-type: text/html", "\n\n";  # MIME header.
    print "<HTML>", "\n";
    print "<HEAD>";
    print "<TITLE>Perl meets the World Wide Web</TITLE>", "\n";
    print "</HEAD>", "\n";

And here is the HTML ender code:

   print "</BODY>", "\n";
    print "</HTML>", "\n";

How can you avoid having to type these lines in every program? The easiest way is to store the repetitive code in separate files and let Perl open the files and read them into your program. This, in a nutshell, is what the require function does.


TIP:  If you’ve ever dabbled in C or C++, you will recognize that require operates nearly identically to the #include directive.

Creating an Ender and Header Code File

Wondering how to take advantage of these two new concepts? Here’s one way:

1.  Place the header code in a file.
2.  Place the ender code in another file.
3.  Use require to call the two files in at the appropriate spots in your program or to place any require statements at the beginning of the program.

Regarding step 3, the latter—placing all require statements at the beginning of the program—is the best choice for a couple of reasons:

  All of your program’s external code; that is, code coming from some other file, is consolidated in one place. It’s easier for you to keep track of it that way.
  It is possible—and quite common—to write libraries of functions that you can call from a Perl program. Thus, a common assumption is that the files you are calling in with require contain one, several, or hundreds of subroutines. The require command defines the subroutines for you, but they aren’t available to you until the definition is made; all you have to do after that is use them.

Let’s start by making a subroutine out of the ender code. To do so, open your text editor and type these lines:

   #!c:/perl/bin/perl

   # Set up a standard HTML footer section.  At this point, it simply
   # ends the BODY and HTML sections.

   sub HTML_Footer
   {
       print "\n", "</BODY>", "\n";
       print "</HTML>", "\n";

   }                   # End HTML_Footer

Save the file as htmlend.pl.

Dissecting the Ender Code File

Several new ideas pop up in this example. But for the most part, it looks like the previous Perl programs you’ve written. Notice the line

   sub HTML_Footer

The sub statement defines the following code as a subroutine: specifically, all the code between the open brace ({) on the next line to the closed brace (}) at the end. HTML_Footer is the name of the subroutine. You will use it to call the code.

A word on the code itself: the print statements, which should be quite familiar to you by now, open a line in the HTML document you are creating, end the body of the document, and then drop down one more line. </HTML> tells the Web browser that no more HTML code follows.


NOTE:  The open and closed braces delimit what is known in Perl (and many other programming languages) as a block of code. This is a fundamental concept in Perl. Remember 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.