Print this Article (NS4)
Netscape Navigator
Internet Explorer
Opera
Neoplanet

Forums
HTML
General
Site Dev
Programming
Flash
Grafix (Art)

Laboratory
Smart HTML
Color Lab
Generators

Contents
Simple CGI
  1. Hello World
  2. Print function
  3. Quoting, EOF
  4. Metacharacters
  5. Special Characters
Perl Basics 1
  1. Variables
  2. Arrays
  3. Hashes
  4. Split function
  5. Subroutines
  6. Defaults
Form CGI
  1. Loops
  2. Conditions
  3. Boolean Statements
  4. Pattern Matching
Time CGI
  1. Local Time
  2. GM Time
  3. time function
Perl Basics 2
  1. Reading Files
  2. Writing Files
  3. Including Files
  4. chop function
  5. chomp function
Guestbook CGI

Redirect CGI

Poll CGI

  1. Giving Commands
  2. Voting
  3. Results Display
  4. Adding Your Vote
Password CGI
  1. Authentification
  2. Multiple Users
  3. Encryption
Mailing List CGI
  1. Sendmail
  2. Multiple Recipients
Unlimited Subdomains CGI

News Grabber CGI

  1. LWP::Simple
Message Board CGI (Part 1)

Back to the Top


Perl Case Study - Simple CGI
By Lisa Hui

#!/usr/local/bin/perl
#################
# File Name: simple.cgi
# This test script was created to demonstrate
# how Perl/CGI syntax works. This is also an
# introduction to the subject of Perl itself.

# What this script does:
# Print out a HTML page.
#################
# Print the HTTP Header
# You have to print this out first so that the browser
# knows that the following output is HTML

print "Content-type: text/html\n\n";

# Print a Hello World HTML page:

print qq|
Hello World!
|;


See the script in action: simple.cgi

The good news: it's just that simple!

Here you've seen a full-fledged Perl CGI that works on on a UNIX server (given that perl is installed). There are 5 elements demonstrated here which are useful to know.

The first line is rather important. It points to your perl interpreter (the stuff after the first #!). And this may actually vary from what I've written above. How to find out: if you have telnet, type which perl at the prompt.

The number sign # works much like a // comment line does in C++. It basically tells the perl interpreter to disregard that commented line of text. As you can see from the script, putting multiple #s in a row don't make a difference (just done for aesthetic purposes). So near the top I've included a little file description, all commented out.

The first line that was printed onto the screen is "Content-type: text/html\n\n" which just happens to be a HTTP header. This is required by the server to get this kind of output to the browser. I mentioned a few more HTTP headers in my analysis of Meta Tags. If you were to take out this line, you would be given a nasty 500 Server Error which happens to be one of the most annoying and common errors seen when trying to execute a CGI script [click here to get a list of server error messages with corresponding numbers] .

Another important thing to see is the actual ouput command. Perl uses the print "The text you want to output goes inside these quotes."; syntax. Notice the semicolon; it is VERY important that there is a semicolon at the end of the statement (same goes for C++)!

This print is swell and fine for outputting one line... but what if you wanted to write a whole web page code into it? Quite tedious. So there's another type of printable quoting using print qq* ... *; where (*) refers to any character type (the example used | ) called a handle. So you can write a whole page, spacing and all, without ending on each line and worrying about syntax errors. An added plus is that it isn't even bothered by quotes (").

print qq|  (beginning statement)

|;  (end statement)

Make sure that the character (*) that is used after the qq does not show up before the ending statement (the print routine here truncate as soon as it encounters this character - if it is cut off prematurely, it will serve you up a 500 Server Error message). Some not-so-commonly used characters include | * ~ ^ { and }

A method similar to qq is to use what is called the "here-document syntax:

print <<EOF;
The statements go between the start EOF and the end EOF (which is here). Notice that the semicolon follows the first EOF.
EOF

There's one classic "what if" case I want to mention at the start before you start getting (more) confused. What if the text in the print command has quotes?

Example:
print "The Curious One, George, said in his ape-ish tone of voice, "Can I have a banana?" ";

A better example would've been using quotes in HTML:
print "<a href="thispage.html" target="_none">Click Here</a>";

But in any case, the print function will interpret the first end quote it finds and truncates the line. It will eventually happen so, what can you do to prevent this type of error, apart from eliminating quotes from your all your text?

This is a situation is for super spiffy metacharacters, in particular the backslash ( \ ). It marks the character to its right a literal character (meaning that the print function will interpret a blackslashed quote just as another character to print out!). And here are the two examples with the backslashes added in. These shouldn't give you server errors now:

print "The Curious One, George, said in his ape-ish tone of voice, \"Can I have a banana?\" ";

print "<a href=\"thispage.html\" target=\"_none\">Click Here</a>";

With the advent of backslashed characters, you get special characters like the newline ( \n ). Newline works like <br>, except helps space your characters in the code (not in the browser since it is invisible and counted like whitespace). So you can write something like:

print "<a href=\"thispage.html\" target=\"_none\">Click Here</a>\n";

Below is a list of special characters, escape sequences, and control characters that perl accepts:
Special Character Name
\n Newline
\t Tab
\b Backspace
\e ESC
\r Carriage Return
\a Alert (bell)
\cC Ctrl-C
\Q All following non-alphanumeric characters are automatically backslashed
\L Forces all following characters to be lowercase
\U Forces all following characters to be UPPERCASE
\E Ends any trailing \Q, \U, or \L characters
\l Forces the next character to be lowercase
\u Forces the next character to be Uppercase

A piece of trivia at this point: a list of metacharacters \ | ( ) [ { ^ $ * + ? .


Perl Basics: Becoming a Perl Newbie Extraordinaire

©1999 Team 26297 "Ad Infinitum Web." All rights reserved. Any reproduction of this document for commercial or redistribution purposes without the permission of the author is forbidden.