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


Lists of Strings

When you load strings into an array, they need to be distinguished somehow. The Perl convention departs slightly from what we have learned so far, which is to enclose strings of characters in double quotes. This can be done with lists, but it is considered more correct to delimit lists of strings with single quotes ().

Table 1.2 illustrates some of the things you can do with strings.

Table 1.2: Perl List Examples

Initialization Comment

@list = (4..8); same as @list = (4,5,6,7,8)
@list1 = (‘red’, ‘green’, ‘blue’); array of colors
@list2 = (1, ‘yellow’, @list1); same as @list2 = (1, ‘yellow’, ‘red’, ‘green’, ‘blue’);
@list3 = (); null (empty) list
@list4 = (0,1, @list3, 3); same as @list4 = (0,1,3);

Perl lists have numerous other features, but we’ll save those for when we approach more complex programming topics. For now, you should know what a list is, how to initialize it, and how to access one of its members.


NOTE:  Presumably, we’re all fully qualified computer nerds here, so we are allowed to use “access” as a verb. Be advised, however, that the practice in common usage drives English-language purists to scowling fidgets.

Perl and the Common Gateway Interface

You’ve learned a little about the Perl programming language. But how does it fit into the World Wide Web? The Common Gateway Interface (CGI) is the key. CGI has been used for many years as a facility for passing information from a Web page to a program that can process the information.

CGI, despite what many programmers put on their resumes, is not a programming language. It is, as the name states explicitly, an interface. It allows you to write a program that will take all of its input from an HTML document, a page on the World Wide Web, and do something with that input. You can regard CGI as a kind of pipeline between your Web page and a Perl program (see Figure 1.9): Whatever is entered on the page is available to your program through CGI.


Figure 1.9:  A search phrase or a list selection entered in this form will be processed by a Perl program through CGI.

HTML is quite good at describing how a Web page should look in a browser, but the language all by itself has virtually no facilities for processing information or even making rudimentary decisions.


WARNING:  Some browsers include extensions written to HTML that support all kinds of fancy interpretation. In the real world, however, you cannot depend on your Web site visitors possessing the latest and greatest browsers with all of their non-standard HTML extensions.

When you run a Perl program from the command line, it takes its input, generally, from you, at your keyboard, and it sends its output, generally, back to you, at the screen. CGI reroutes those standard conventions. The Perl program’s input comes from the Web page. Most importantly, CGI sends your program’s output back to the Web server. If the output happens to be formatted correctly in HTML, the server will put it out as an HTML document to whatever browser is connected to it. In other words, a print statement from within your Perl program will be printing to the Web server, not the screen (see Figure 1.10).


Figure 1.10:  The difference between “standard” and CGI output

This is a difficult concept for many neophytes to grasp, but it is the foundation of using CGI as a pipeline between Perl and HTML. You can draw a Web page from a Perl program. And, because Perl is a fully functional programming language, rather than a markup language such as HTML, you can decide within your program what to draw based on what has been entered in the page and sent to you.

Of course, this facility isn’t limited to Perl. You can interface with CGI using any program written in any language (provided, of course, that it will run on your computer!). Indeed, there may be occasions when you need the brute force of C/C++ or some other high-level compiled language to tackle some process that would bring your Web server to its knees if the program were written in Perl. For example, a program that does a lot of heavy number crunching would be much more efficient in C or C++ than in Perl. Those occasions will be rare, however. Most of what you need to do can be accomplished more easily from a Perl script than from a compiled program. Additionally, your Perl program won’t have to be rewritten and recompiled if you move to another operating system or computer platform.


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.