Examples Home to Cookwood Press


(page 18--see Scalars Chapter)

Perl Building Blocks

The files from this chapter can be found in the basics folder.

page section HTML Perl
38 Creating Output for a Browser animal.html animal.cgi
39 Documenting Your Script comments.html
40 Checking the Script’s Syntax -- badmonkey.cgi

About Servers, Perl, and CGI.pm

There are no example files from this chapter.

Running Perl CGI on a Unix Server

The files from this chapter can be found in the unixserver folder.

page section HTML Perl
51 What About CGI.pm? -- getversion.cgi
58 Testing Your Unix Server -- unixtest.cgi

Testing Scripts Locally on Windows

The files from this chapter can be found in the winserver folder.

page section HTML Perl
66 Testing Your Server (Part 1) test.html none
69 Installing Scripts on Windows Servers -- perltest.pl
70 Testing Your Server (Part II) -- perltest.pl

Testing Scripts Locally on the Mac

The files from this chapter can be found in the macserver folder.

page section HTML Perl
77 Testing the Server (Part 1) test.html none
81 Testing Your Server (Part II) -- test.cgi

Getting Data from Visitors

The files from this chapter can be found in the dataform folder.

page section HTML Perl
84 Labeling Incoming Data namevalue.html (namevalue.cgi)
86-94 Creating a Form namevalue.html (namevalue.cgi)
95 Using an Image To Submit Data zonemap.html (display_param.cgi)
96 Creating a Link to a Script gettime.html (gettime.cgi)
97 Using a Link to Input Data to a Script getlocaltime.html (getlocaltime.cgi)

Environment Variables

The files from this chapter can be found in the envvar folder.

page section HTML Perl
100 Your Visitor’s Browser and Platform   browser.cgi
101 Viewing Available Environment Variables   envlist.cgi
102 Storing Data from Environment Variables   storeenv.cgi

Getting Data into the Script

The files from this chapter can be found in the input folder.

page section HTML Perl
104 Getting Single-Valued Form Data singlevalue.html singlevalue.cgi
105 Getting Multiple-Valued Form Data multivalue.html multivalue.cgi
106 Getting All the Form Element’s Names allnames.html allnames.cgi
107 Getting All the Names and Values alldata.html alldata.cgi

Simple Operations with Scalars

The files from this chapter can be found in the scalars folder.

page section HTML Perl
110-111 Assigning a Value to a Scalar Variable   assignment.cgi
112 Multiplying, Dividing, Adding, Subtracting math.html math.cgi
113 Using More Than One Operator   precedence.cgi
114 Raising a Number to an Exponential Power exponents.html exponents.cgi
115 Using Mathematical Functions sqrt.html sqrt.cgi
116 Getting the Remainder of a Division modulus.html modulus.cgi
117 Connecting Strings Together concat.html concat.cgi
118 Repeating a String repeat.html repeat.cgi
119 Operating and Assigning in One Step binary.html binary.cgi
120 Incrementing (or Decrementing) a Variable increment.html increment.cgi

Conditionals and Loops

The files from this chapter can be found in the conditionals folder.

page section HTML Perl
126 Creating a Basic Conditional Statement if.html if.cgi
127 Adding Options for False Conditions else.html else.cgi
128 Adding Multiple, Independent Conditions elsif.html elsif.cgi
129 Using Unless unless.html unless.cgi
130 Repeating a Block for Each Item in an Array foreach.html foreach.cgi
131 Loading the Default Variable foreach_default.html foreach_default.cgi
132 Repeating a Block While a Condition Is True while.html while.cgi
133 Repeating a Block While a Condition Is False until.html until.cgi
134 Executing the Block at Least Once dowhile.html dowhile.cgi
135 Repeating a Block a Given Number of Times for.html for.cgi
136 Nesting Conditional Statements nest.html nest.cgi
137 Skipping a Loop Iteration next.html next.cgi
138 Jumping out of a Loop Altogether last.html last.cgi

Working with Arrays

The files from this chapter can be found in the arrays folder.

page section HTML Perl
140-141 Assigning a List to an Array Variable   assign.cgi
142-143 Referring to a Particular Item in an Array gettingone.html



144 Referring to Multiple Items from an Array getmulti.html getmulti.cgi
145 Adding or Replacing an Item in an Array replaceone.html replaceone.cgi
146-147 Adding to the End or Beginning of an Array





148 Removing the Last Item from an Array pop.html pop.cgi
149 Removing the First Item from an Array shift.html shift.cgi
150-151 Replacing More Than One Item in an Array replacemore.html replacemore.cgi
152 Finding the Length of an Array length.html length.cgi
153 Modifying All the Members of an Array modifyall.html modifyall.cgi
154 Sorting Arrays sort.html sort.cgi
155 Reversing the Order of an Array’s Contents reverse.html reverse.cgi


The files from this chapter can be found in the subroutines folder.

page section HTML Perl
159 Using a Simple Subroutine   simple_sub.cgi
160-161 Creating/Calling a Subroutine That Takes Input
input.html input.cgi
162 Using a Subroutine's Return Value return.html return.cgi
163 Setting the Return Value Manually return_manual.html return_manual.cgi

Storing Subroutines in an External File

Calling Subroutines from an External File

external.html external.lib

Working with Hashes

The files from this chapter can be found in the hashes folder.

page section HTML Perl
168 Assigning a List to a Hash assign_hash.html
169 Getting a Value by Using a Key getone.html getone.cgi
170 Adding or Replacing a Key-Value Pair addone.html addone.cgi
171 Getting Several Values Using Keys getmore.html getmore.cgi
172 Getting All of a Hash’s Keys   keys.cgi
173 Getting All of a Hash’s Values   values.cgi
174-175 Getting Each Key and Value in a Hash



176-177 Getting the Pairs in a Specified Order  



178 Removing Key-Value Pairs delete.html delete.cgi
179 Checking to See If a Key Exists exists.html exists.cgi

Analyzing Data

The files from this chapter can be found in the regex folder.

page section HTML Perl
182 Finding Something   match.cgi
183 Finding and Replacing replace.html replace.cgi
184 Seeing and Using What Was Found   whatwasfound.cgi
185 Splitting a Value into Pieces split.html split.cgi
188 Matching a Single Character matchone.html matchone.cgi
189 Matching a String of Characters sequence.html sequence.cgi
190 Matching a Character from a Group class.html class.cgi
191 Matching a Character That’s Not in the Group negclass.html negclass.cgi
192 Using Class Shorthands classshort.html classshort.cgi
193 Limiting the Location anchor.html anchor.cgi
195 Choosing How Many to Match
196 To include optional elements optional.html optional.cgi
197 To match one or more elements plus.html plus.cgi
198 To include multiple optional elements asterisk.html asterisk.cgi
199 To match at least m and at most n elements mandn,html mandn.cgi
201 Matching One Element or Another alternation.html alternation.cgi
202 More on Using What You Already Matched using.html using.cgi

Remembering What Visitors Told You

The files from this chapter can be found in the remembering folder.

page section HTML Perl
208-209 Storing Collected Data in a Hidden Field hidden.html


212-213 Sending a Cookie sendcookie.html sendcookie.cgi
214 Setting a Cookie’s Expiration Date expires.html expires.cgi
215 Limiting a Cookie to a Domain domain.html domain.cgi
216 Limiting a Cookie to a Part of Your Server path.html path.cgi
217 Limiting Cookies to Secure Connections secure.html secure.cgi
218 Reading and Using a Cookie   readcookie.cgi

Printing, Formatting, and HTML

The files from this chapter can be found in the printhtml folder.

page section HTML Perl
222 Formatting Output with Perl format.html format.cgi
223 Creating a Format Pattern for Integers   padding.cgi
226 Changing the Case ucfirst.html ucfirst.cgi
227 Changing Characters tr.html tr.cgi
228 Finding the Length of a String length.html length.cgi
229 Finding Where Something Is in a String index.html index.cgi
230 Extracting One String from Another substr.html substr.cgi
231 Cleaning up the End of a String chop.html chop.cgi
232-233 Formatting Output with HTML   html.cgi
234 Printing Several Lines at a Time   here.cgi
235 Simplifying Paths to Images and Links   base.cgi
236 Outputting Data in a Table   table.cgi


The files from this chapter can be found in the security folder.

page section HTML Perl
238 Reading the Security FAQs

Lincoln Stein’s World Wide Web Security FAQ

Security Issues When Installing and Customizing Pre-Built Web Scripts, by Selena Sol

239 The Problem with Visitor Input


241 Limiting Access to Files



242 Using CGI.pm to Limit Incoming Data limitinput.html limitinput.cgi
243 Avoiding Tainted Data taintcheck_before.html taintcheck_before.cgi
244 Cleaning and Using Outside Data




Files and Directories

The files from this chapter can be found in the files folder. I'm afraid the links below are no longer live. People were just too weird about the comments they posted.

page section HTML Perl
248-249 Opening a File   (writetolog.cgi)
250 Verifying File and Directory Operations   (writetolog.cgi)
251 Writing to an External File writetolog.html writetolog.cgi
252 Getting Exclusive Access to a File flock.html flock.cgi
253 Reading Data from an External File (link appears in output from writetolog.html) readfromlog.cgi
254 Closing a File   (readfromlog.cgi)
255 Renaming a File rename.html rename.cgi
256 Removing a File delete.html delete.cgi
257 Checking a File’s Status exists.html exists.cgi
258 Opening a Directory   (readdir.cgi)
259 Reading the Contents of a Directory readdir.html readdir.cgi
260 Closing a Directory   (readdir.cgi)
261 Changing the Working Directory chdir.html chdir.cgi
262 Creating a Directory mkdir.html mkdir.cgi
263 Changing Permissions from within a Script chmod.html chmod.cgi
264 Removing a Directory rmdir.html rmdir.cgi

Getting Ready to E-mail Output

Sending Output via E-mail

formmail.html formmail.cgi

Uploading Files

The files from this chapter can be found in the uploading folder.

page section HTML Perl
270 Creating a Form for Uploading Files uploading.html (uploading.cgi)
271 Getting the Name of the Uploaded File filename.html filename.cgi
272 Finding out a File’s MIME Type mimetype.html mimetype.cgi


Specifying Where the File Should Be Saved

Reading in and Limiting Uploaded Files

uploading.html uploading.cgi


The files from this chapter can be found in the debugging folder.

page section HTML Perl
280 Creating an Error Subroutine   error.lib
281 Using Perl’s Error Reporting   die_carp.cgi
283 Isolating the Problem   isolating.cgi
284 Following a Variable’s Progress   progress.cgi

Perl and CGI Resources

page section URL
310 Text Editors

BBEdit for Macintosh

UltraEdit for Windows

Elizabeth Knuth's Review of Windows Text Editors

311 Telnet Programs

BetterTelnet for Macintosh

Microsoft Telnet for Windows

312 Other Folks' Scripts

The CGI Resource Index


The WebScripts Archive

313 Learning More





online documentation