![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Consolidating the CodeThe 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 whos ever done any HTML programming. The exciting part of all this is that the HTML code was generated from a program. It wasnt just typed into a text editor and executed by a Web browser. You created it on the fly.
Youll 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, lets introduce a couple of Perl concepts that will make subsequent projects a little easier: the subroutine and the require function. Perl SubroutinesPerls 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, lets 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 inventedto 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.
Webster defines hubris as wanton arrogance arising from overbearing pride or from passion. After youve been programming for a while, youll know what they mean. In beginners 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 CommandThe hellowww.pl program doesnt 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:
Lets 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.
Creating an Ender and Header Code FileWondering how to take advantage of these two new concepts? Heres one way:
Regarding step 3, the latterplacing all require statements at the beginning of the programis the best choice for a couple of reasons:
Lets 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 youve 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.
|
![]() |
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. |