Mike's Web

Lesson 1

  1. Basics of a Perl script. Writing your first script
  2. Basics of a CGI script. Writing your first CGI.


Basics of a Perl script

You probably are already familiar with HTML, and so you know that certain things are necessary in the structure of an HTML document, such as the <head> and <body> tags. Perl is somewhat similar in that it needs one statement as the first line of every script, telling the server that this is a Perl script, and where to find the Perl interpreter. In most scripts the statement will look like this:

    #!/usr/bin/perl

There should generally not be anything after this statement. If you aren't sure where Perl lives on your system, try typing this command:

    which perl
If the system can find it, it will tell you the path name to Perl. That path is what you should put in the above statement.

Beyond the first line, you'll write your perl code. Perl is case sensitive, meaning that 'x' is not the same as 'X'. Also, most lines of Perl code must end in a semicolon (;), unless it is a loop or other special conditional structure. We'll cover those later.

Let's write a simple first program. Enter the following lines into a new file, and call it "first.pl".

   #!/usr/bin/perl

   print "Hello, world!\n";

Save the file. Now, from the Unix shell, you'll need to type:

  chmod 755 first.pl
This fixes the file permissions (via the change mode command) so that the script can be executed (run). You will have to do this every time you create a new script; however, if you're re-editing an existing script, the permissions should remain the same.

Now, to run the script, just type:

  ./first.pl
If all goes well, you should see it print Hello, world! to your screen.

Now, let's say you want to have a few variable names in your script. A variable is just a name that stores some value in it, so you can refer to it or manipulate it throughout your program. Perl variables are prefixed with a dollar sign ($), so for example, $x, $y, $z, $username, and $url are all examples of variable names.

Let's edit first.pl again and add some variables to it:

Save, and run the script just as before. Notice the results? First a few comments. This line:

actually reads input from your keyboard. The program will pause and wait for you to type something in, followed by a carriage return. Whatever you typed is stored in the variable $you. Since $you also contains the carriage return itself, we use

to remove the return from the end of the string you typed in. (the chop function simply removes the last character from any string.) The following print statement:

substitutes the value of $you that you entered in , as well as the value of $name that was defined earlier in the script. The "\n" at the end if the line is the perl syntax for a carriage return.


Basics of a CGI script

A CGI program is still a Perl script. But one important element of many CGI's is, that the CGI generates a web page (such as a form-processing CGI, that returns a "thank you" page.) If you are writing a CGI that's going to generate a HTML page, you must include this statement somewhere in the script, before you print out any HTML tags:

This is a MIME header that tells the receiving web browser what sort of data it is going to get.

Now let's take the original program that was in first.pl, and make it into a CGI script that displays a web page. If you are running this on an io.com account, or another Unix that lets you run CGI's in your public_html directory, you will probably need to rename the file to first.cgi, so that it ends in the .cgi extension.

Here is what it should look like:

Now, first save this file and run it in the Unix shell, like you ran the other one. Notice how the script will just print out a bunch of HTML? This is what it should do, BUT, very importantly, if there's an error in your script, the perl interpreter in Unix will tell you exactly what line the error is on. This is good to remember in the future, because when you're writing longer, more complex scripts, you may get bugs or errors, and the error message you get on the web site (500 Server Error) is not useful at all for debugging.

Now you'd like to use this script as an actual CGI. You don't need to call it from a web page. Just move it into your public_html or public_web or CGI-bin directory, and call it directly. For this example, here's my URL:

Another way to write the above CGI, without using multiple print statements, is as follows:

Note that there are no spaces between the << and the EndOfHTML, in this statement:

Also, there should be no spaces before the closing EndOfHTML - it must be flush left against the left margin. (If you get an error along the lines of "Can't find string terminator EndOfHTML", this means you forgot to move the EndOfHTML to the left margin.)

This manner of outputting HTML will become more useful with future CGI's, because it doesn't require you to escape embedded quotes, like you would with a normal print statement:

Note that the quotes around the URL have to be escaped with a backslash in front of them for it to work.

Lesson 2 will cover parsing environment variables, so you can do something useful with CGI's.

Back to Index


Copyright © 1997 by Jacqueline D. Hamilton All Rights Reserved.


Mike's Home | Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4 | Lesson 5 | Lesson 6 | Generic Lesson