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


Perl’s Ancient History

There is only one reason that Perl programs—or scripts, which is a lexical convention that will be explained shortly—are so universal in World Wide Web programming. The simple fact of the matter is, until the last few years, virtually every Web server in existence was running on a UNIX system and Perl is among the most useful of UNIX tools.

The first Hypertext Transfer Protocol (HTTP) servers were written for UNIX, too, and freely distributed among system administrators who wanted to try out the World Wide Web. CGI was developed as a standard of communication on these systems. In a sense, Perl and HTTP and CGI all became standards for doing Web work (see Figure 1.2).


Figure 1.2:  The HTTP-CGI-Perl connection

The beauty of standards is that they usually transcend the platforms on which they originated. The number of non-UNIX Web servers and Web sites on the Internet increases every day. Yet the HTTP-CGI-Perl connection remains the same because it was lifted intact into the newer platforms.

UNIX is, in a very large sense, an operating system written by and, most importantly, for programmers. It really was never intended for humans to use easily, which is why so many people have gone to such extraordinary lengths to make UNIX more friendly, with X Windows and various other graphical interfaces. These interfaces require tremendous amounts of processing power, so in many cases system designers have simply given up and relegated bare-bones UNIX to the background, running it on the system server and hanging Macintoshes or other workstations running Windows on the network for users.

The beauty of UNIX for those who have taken the time to learn it is in the rich set of software tools that it provides. Unadorned UNIX is like a box of wonderful Swiss Army knives; with any one of them, you can carve any masterpiece your imagination can conjure.

Perl is one of the most useful of those Swiss Army knives.

The UNIX Toolbox

Consider some of the more obscure tools you can pull out of UNIX:

  grep Allows you to search through files, directories, or entire disks for words or phrases.
  sh, csh, ksh Some of the UNIX “shells,” which are akin to the MS-DOS command line but considerably more powerful. Shell scripts are like DOS batch files with turbochargers attached. You really don’t need another programming language.
  ed, sed, vi The UNIX editors that everyone hates…and everyone uses.
  whereis Finds files anywhere; actually a shell script.
  man Calls up the manual pages for programs and other utilities, often serving to further confuse the hapless user.

We Owe It All to Larry Wall: A History of Perl

Larry Wall is a linguist-turned-programmer who as of this writing was an associate at O’Reilly & Associates, a technical publishing company. Legend has it that he began working on Perl nearly 10 years ago while attempting a sticky project for Unisys.


NOTE:  The Perl language grew out of the classic UNIX philosophy: If the system doesn’t allow you to do your job easily, then you simply write another tool to solve the problem.

Perl actually is an acronym whose most accepted expanded version is Practical Extraction and Report Language, though UNIX wags have come up with many more earthy descriptions, such as “Pathologically Eclectic Rubbish Lister.” It was derived in large part from sed and awk, jackhammers of the UNIX toolbox for those who understand them, utterly unintelligible command programs for those who don’t. After all, what can one say about a program whose most famous error message is awk: bailing out near Line 1?

The strengths of sed and awk, and their offspring Perl, lie mainly in their built-in capabilities for processing text through pattern-matching, searching for and replacing phrases—or “strings”—in entire groups of files, and the use of UNIX’s obscure yet extremely powerful regular expressions, which are discussed in full in Skill 5.

Regular Expressions: Bane and Boon

Regular expressions are among the most useful—and most difficult to master—tools in the UNIX array.

You can think of them as supercharged search-and/or-replace operations. Where most any text editor will let you find phrases and replace them with other phrases throughout a file, regular expressions add a great deal of power to the operation. For example, you can use regular expressions to look for strings at the beginning or end of a line, or in a word, or for a specific number of occurrences.

But it’s not easy. A Perl regular expression that swaps the first two words in a line of text looks like this:

        s/^([^ ]*) *([^ ]*)/$2 $1/;

Doesn’t make much sense, does it? But that could be a very useful operation, couldn’t it?

We’ll defer a full explanation of regular expressions until Skill 5. For now, let’s just say that you will find many uses for them.

The bedrock of UNIX is the C programming language—most of it is written in C—but C, in its position at the foundation of the operating system, adheres to the minimalist philosophy of UNIX, which means that you often have to write scads of C code to accomplish relatively simple tasks. A trivial search-and-replace operation on a text file, written in C, requires the programmer to at least scan the file character by character and could easily grow from a simple subroutine into an entire application (see Figure 1.3).


Figure 1.3:  An example of code written in C++

But the same operation can be accomplished in a few lines of Perl code (see Figure 1.4).


Figure 1.4:  An example of code written in Perl


TIP:  Perl is a challenge to learn, but it is infinitely more efficient for the programmer (read: “fewer lines of code”) and easier to use than C.

UNIX programmers snapped up Perl as a tool of choice almost immediately for doing tasks ranging from “quick and dirty” to horribly complex. Because you have the ability to call most of the standard UNIX system services from a Perl script, including the internetworking functions, you probably could write an entire operating system in it. It would be very slow, but it would run a computer.

To this day, almost every serious UNIX systems programmer works with Perl almost daily. It’s just too useful for programmers to ignore.


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.