Week 12 - Introduction to Perl & CGI

The docs we will be using for the third part of the semester is the Perl manpages, avaliable at the Perl Website (under the "Documentation" link). If you want to download the docs for offline viewing, you can grab a giant tarball of the HTML docs.

Additional links and (hopefully) helpful info can be found on the resources page. As always, I welcome submissions if anybody's got a link to another site that they think would be useful.

If you want a quick reference to Frequently Asked Questions, have a look at the Perl FAQ.

CGI Concepts

Remember when we talked about the client-server paradigm with Java? Well, we're back to it, only this time we're on the other end: CGI scripts run on the server-side. This is actually kind of nice, because the biggest problem with the client-server paradigm is the client; it's a mess to maintain. Being able to keep things on the server is a lot easier to administer.

The only thing that happens on the client-side is that the user views HTML through a browser. That's it. No messy Virtual Machine, no Applets or class files, just HTML. Kind of nice.

CGI Requirements

CGI programs can be written in any programming language, provided that said language meets the following requirements:

  1. It must be able to read environment variables.
  2. It must be able to read from stdin and write to stdout.
  3. It must be standalone (i.e. the OS can execute the program without needing to specify an interpreter or any cmd-line arguments)

That's all there is to it. As you may have already guessed, Java does not make a very good CGI language because it can't read from environment variables. (Or at least it couldn't last time I checked.)

Perl Culture

Perl Origins

Perl was not developed by a standards committe (like HTML), nor was it developed by a large corporation (like Java). Rather, it was invented by a mild-mannered fellow by the name of Larry Wall. Larry actually has a stronger background in linguistics than he does in computer science, and that background has helped him a great deal in the creation of Perl.

What Perl Borrows From

The designer of Perl, Larry Wall, didn't really set out to make a whole new language so much as he set out to make something that would take the traits he liked best from a whole bunch of other languages and roll them into one, big, ball of goo.

The following are the main influences of Perl.

In addition to these, there are some other languages which made smaller contributions (please don't flinch when you see any of these): C++, BASIC, FORTRAN, Prolog, LISP, Ada, and others.

Perl Slogans and Sayings

In the absence of any formal marketing campaign, there are a few slogans and sayings that have been ascribed to Perl.

Perl Language Features

The good news is, if you're familiar with C, shell programming and regular expressions, you're well on your way to knowing about Perl.

The following list summarizes some of the high points. I will discuss each of these in class but I'm not going to go into any detail here. Links are provided along the way that will take you to the appropriate man page for further enlightenment.

Hello World

Command-Line Version

Here's how you do Hello World in Perl:

 print "Hello World" 

Tricky, eh? If you were to save the above program in a file called hello.pl, in order to run it you need to type perl hello.pl (somewhat similar to the way you invoke Java programs from the command line).

If you want to make a standalone program, include the following line at the top of the file:


That line tells the operating system where to find the interpreter that will be used to run the script that follows. This is typically known as the "she-bang" line. (sharp + bang, get it?)

Then, change the file to executable by typing chmod +x hello.pl. Thereafter, you can execute the program simply by typing ./hello.pl. (Hopefully, all of you remember how to do this from your UNIX class.)

See perlrun for more info on running the interpreter.

CGI Version

The CGI version looks like this:


print "Content-type: text/html\n\n"; # It is VERY important to have TWO \n's
print <<END_HTML;
<title>CGI Hello World</title>
<p>Hello World!

The first line dscribes the content that will follow and is the first line sent to the browser. It is VERY IMPORTANT to have that line as the first thing printed before you print any HTML. It is likewise important that you have exactly TWO newlines following the "Content-type" line. If you are having problems with your CGI program, test these things and make sure they are working properly.

That business you see with the <<END_HTML is called a "here document" and it's convenient when you want to print things that will span multiple lines. Perl borrows this from the shell.

Proposal For Assignment # 3

Check out the info for Assignment # 3. What I need from you all by next week is a proposal for what kind of CGI program you want to do. Make a text file called "proposal3.txt" and put it in your home directory.


3/31/99 - Initial revision
4/6/99 - Added the third specification for CGI programs
4/27/99 - Added another pithy Perl slogan