Path // www.yourhtmlsource.comCGI Scripting → BASIC PERL

Basic Perl


Perl is the language most developers favour when writing their CGI scripts. The functions we’ll need are simple enough to get to grips with, but Perl is a fully-fledged programming language. Don’t worry about it though, you don’t need any programming experience to get started.

Clock This page was last updated on 2012-08-21



The Possibilities

So what are we learning Perl for? After reading the following three or four tutorials you are going to be able to have your HTML pages interacting with your readers and server, allowing you to properly deal with forms, generate dynamic pages, counters, guestbooks and email senders. You’ll be able to host these features of your site along with the rest of your stuff and not have to rely on some third-party service on another server, and so have much more control over them.

What this really does is allow you to set up some great features for your site that will make it feel much more complete and professional. You won’t be running someone else’s guestbook, you’ll be running your own, fully-customised version. Furthermore, having some CGI programming experience will allow you, later, to set up complex databases to generate websites on the fly, should this ever take your fancy; and makes learning extra programming languages in the future far easier.

Some Definitions

Let’s get a handle on what these acronyms mean, eh? CGI stands for Common Gateway Interface, which means that the CGI script is the ‘gateway’ between your HTML pages and your server. CGI itself is not a programming language, but a method of communication between your browser and server. It is possible on the majority of web servers. Values can be passed between them to update your webpages or to update server-side files. The CGI gets the data from one source and can then send it to a database, or document, or directly to the reader.

PERL stands for Practical Extraction and Report Language. It was created by a man named Larry Wall, who had been told to create a program that would allow computer systems at both sides of America to communicate. It is sort of an amalgam of a number of simpler languages, and is a full programming language (HTML or JavaScript are not).

Perl is a very flexible language, allowing multiple ways of achieving the same outcome, which makes it very adaptive to your skill level. If you want to have a good understanding of Perl, there’s no substitute for buying a good book. I picked up » Programming Perl, which was penned by Larry Wall himself, and is an excellent guide and reference to the language and its practical applications.

What do I Need?

If you write HTML pages you probably have all that you need to start spinning some CGI scripts. All that’s required is a basic text editor — as always NotePad or SimpleText are the recommended programs. You need to be careful if you use an assistive editor (like HomeSite or Arachnophilia), as since these editors are geared-towards creating HTML files, they may add in things to your files that won’t go down well in Perl. Try to use NotePad.

Perl is run a little differently to HTML, as it needs to be interpreted by a program on the server as it runs. Programming languages fall into two categories — they’re either compiled or interpreted. If code is compiled, a program called a compiler converts your code into code that can be read by a computer (called machine code). If you’ve made any errors the compiler will tell you about them at this stage.

Interpreted languages like Perl only undergo this conversion when they’re run, and so this is when you find your mistakes. This means that you have to thoroughly test your CGI scripts online before you start using them. The benefit to all this is that Perl scripts are easy to modify and portable — you can move them between sites and they’ll still work.

If you’d prefer not to test all your scripts online, you can download a copy of PERL (the interpreter) from » Perl.com.

The resulting server setup you’ll need is the main sticking point. For one, the people that host your site will need to provide access to PERL. If you’ve paid for your hosting then you should expect this. PERL is pretty widely available so a lot of ISPs are offering this service now too. If they have installed PERL on your server then it should be no problem to create and upload scripts. Once they’re up there, you’ll need to be able to change their permission settings. We’ll run through that later.

If the option is available, ask your hosting company to give you access to the server error log. This will help you diagnose problems when your script won’t run, as PERL itself isn’t very helpful in showing you where you went wrong.

Finally, you’ll need to know the path to PERL. This is basically the address of the interpreter on your server, so the files will know where to go when they’re run. Again, this can be found from your hosting people. Most providers will go along with the traditional value of /usr/bin/perl. The Perl scripts that you write are just a list of instructions for the interpreter to carry out.

So, have everything above? Great; let’s get started...

My First Script

Perl scripts don’t really seem to do much at first, so don’t be expecting fireworks for a tutorial or two. You’ll need some experience with programming before we start hitting on the major features. For now, let’s have a look at a basic Perl script syntax.

First, we declare where the script will find the PERL interpreter, using the address we should have found out. The first line in your file will be

#! /usr/bin/perl

Among Perl programmers this line is known as the ’shebang’ line. Now we’ll write a short script that will generate a HTML page with a message on it. We’ll then upload it, change its permissions, and run it.

The next line under the interpreter declaration is a note to your browser that it will be receiving, through the CGI, a HTML page. This line is called a content header. Then we go ahead and give it some HTML code, through Perl’s print "" command. Our first script flows thusly:

#! /usr/bin/perl

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

print  "<html><head><title>My First Script</title></head>\n\n";
print  "<body>\n";
print  "<p>Hello world!</p>\n";
print  "</body></html>";

As you can no doubt see, this is just a basic HTML page. Everything between the print "" quotes will be sent to your browser. The \n things are how Perl signifies line-breaks. If you view source on the generated page, you’ll see that the code skips lines. Also, it is part of Perl syntax that each line ends with a semicolon. Forgetting these is a very common programming error, so make sure you’ve put them all in.

sourcetip: If you need to put your own quotation marks into the HTML page, you need to ’escape’ them, so that the print "" command isn’t interfered with. To do this, just put a backslash before each one, like so: \".

Now, we save the file as firstscript.cgi — remember how to do this from your early days in HTML? Save the file as text-only, and give it the .cgi extension. There; we now have a fully-functioning CGI script. Not too painful, eh? All that remains is to upload and run.

sourcetip: Your scripts can also be given the Perl extension, .pl (e.g. firstscript.pl). Both file types function the same way.

Getting it up

You can transfer your CGI files to your server just as you have transferred your HTML files in the past, with an FTP program. There may be a root directory called the cgi-bin located at somewhere like

http://www.yoursite.com/cgi-bin/

This is a common place for your scripts to be placed, but on some servers it isn’t necessary. Usually your server is configured to recognise all files placed into this directory as special script files. You can create your own if one doesn’t already exist.

As it stands, your server can’t interact with this CGI yet, as for security reasons it’s not allowed to just run arbitrary scripts. To allow this we have to modify the file’s ’permissions’ to allow the server to execute it. Find the chmod command in your FTP client, and set the file’s permissions to 755.

chmod 755 script.cgi

Permissions are a mechanism in UNIX to stop certain users from running programs that they’re not supposed to. 755 in this case would mean that:

  • 7 — Owner can read, write and execute
  • 5 — The group can read and execute, but not write
  • 5 — Everyone else can read and execute, but not write

Anyway, you don’t really need to know too much about this — just that setting the mode to 755 allows your server to run the script, but will allow only you to modify it. You can chmod an entire directory if you want, which is a good argument for setting up a separate directory for your scripts.

Now just open the script by typing the file’s address into your browser’s address bar. You can also see how my script turned out. Assuming your code was error-free, your browser should pull up our simple HTML page. How cool is that?

Ahem, yeah I know. Still — it gets better in a minute!