11.07.2015 Views

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Perl 365Further ReadingAdobe Creative Team. Adobe Acrobat 8 Classroom in a Book. SanJose, Calif.: Adobe Press, 2007.Adobe Reader (free download). Available online. URL: http://www.adobe.com/products/acrobat/readstep2_allversions.html.Accessed November 4, 2007.Lowagie, Bruno. iText in Action: Creating <strong>and</strong> Manipulating PDF.New York: Manning Publications, 2007.Padova, Ted. Adobe Acrobat 8 PDF Bible. Indianapolis: Wiley, 2007.Planet PDF: The Home <strong>of</strong> the PDF Community. Available online.URL: http://www.planetpdf.com/. Accessed November 4,2007.PerlThe explosive growth <strong>of</strong> the World Wide Web has confrontedprogrammers with the need to find ways to linkdatabases <strong>and</strong> other existing resources to Web sites. Thespecifications for such linkages are found in the CommonGateway Interface (see CGI). However, the early facilitiesfor writing CGI scripts were awkward <strong>and</strong> <strong>of</strong>ten frustratingto use.Back in 1986, UNIX developer Larry Wall had created alanguage called Perl (Practical Extraction <strong>and</strong> Report Language).There were already ways to write scripts for simpledata processing (see scripting languages) as well as ah<strong>and</strong>y pattern-manipulation language (see awk). However,Wall wanted to provide a greater variety <strong>of</strong> functions <strong>and</strong>techniques for finding, extracting, <strong>and</strong> formatting data. Perlattracted a following within the UNIX community. Sincemuch Web development was being done on UNIX-basedsystems by the mid- <strong>and</strong> late-1990s, it was natural thatmany webmasters <strong>and</strong> applications programmers wouldturn to Perl to write their CGI scripts.As with many UNIX scripting languages, Perl’s syntaxis broadly similar to C. However, the philosophy behindC is to provide a sparse core language with most functionalitybeing h<strong>and</strong>led by st<strong>and</strong>ard or add-in programlibraries. Perl, on the other h<strong>and</strong>, starts with most <strong>of</strong> thefunctionality <strong>of</strong> UNIX utilities such as sed (stream editor),C shell, <strong>and</strong> awk, including the powerful regular expressionsfamiliar to UNIX users. The language also includesa “hash” data type (a collection <strong>of</strong> paired keys <strong>and</strong> values)that makes it easy for a program to maintain <strong>and</strong> checklists such as <strong>of</strong> Internet hosts <strong>and</strong> their IP addresses (seehashing).Wall made it a point to solicit <strong>and</strong> respond to feedbackfrom Perl users, <strong>of</strong>ten by adding features or functions.Wall’s approach has been to provide as much practical helpfor programmers as possible, rather than worrying aboutthe language being well-defined, consistent, <strong>and</strong> thus easyto learn. For example, in most languages, to make somethinghappen only if a certain condition is not true, onewrites something like this:If ! (test for valid data)Print Error-Msg;Else Process_Data;In Perl, however, one can use the “unless” clause. Itlooks like this:Unless (Test for invalid data) {Process_Data;}Syntactically, the unless clause does not provide anythingmore than using an If <strong>and</strong> Else would, <strong>and</strong> it involves learninga different structure. However, it has the practical benefit<strong>of</strong> making the program a little easier to read by keeping theemphasis on what the program expects to be doing, not onthe possible error. Similarly, Perl <strong>of</strong>fers an “until” loop:Until (Condition is met) {Do something;}In C, one would have to sayWhile (Condition is not met) {Do something;}This “Swiss army knife” approach to providing languagefeatures has been criticized by some computer scientistsas encouraging undisciplined <strong>and</strong> hard-to-verify programming.However, Perl’s many aficionados see the languageas the versatile, essential toolbox for the ever-challengingworld <strong>of</strong> Web programming. As the language evolvedthrough the late 1990s, it also added a full set <strong>of</strong> object-orientedfeatures (see object-oriented programming).Sample Perl ProgramThe following very simple code illustrates a Perl programthat reads some lines <strong>of</strong> data from a file <strong>and</strong> prints themout. The first line tells UNIX to execute the Perl interpreter.The file name data.txt is assigned to the string variable$file. The file is then opened <strong>and</strong> assigned to the variableINFO. A single statement (not a loop) suffices to assign allthe lines in the file to the array @lines. The “foreach” statementis a compact form <strong>of</strong> For loop that assigns each linein the array to the string variable $line <strong>and</strong> then prints it tothe screen as HTML.#!/usr/local/bin/perl$file = ’data.txt’;open(INFO, “

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!