How To Write A PERL Program

More and more I get requests for help with the formmail and captcha programs here at bumblebeeware. However, the people have no idea what they are doing or where to even begin. As a lifetime programmer, you forget that people don't understand everything.

So here is a walk through of a basic program in perl. This should have you understanding and writing perl in about 15 minutes.

First understand that a perl program is nothing more than a text file. You can use almost any text editor to write a perl program. Once you upload the program to your server, the perl compiler will read the program and convert the text into computer code so the program actually does what you want it to do.

The first element of every program is the path to that perl compiler. You will see a line like, #!/usr/bin/perl at the begining of every perl program. That tells the web server where to find perl in the operating system and ad that it should use perl to read and execute the script.

So our program starts out with:
#!/usr/bin/perl

The next thing we need to do is have the program do something. Since we want to print the results to the browser we need some print commands and the content type. HTML is text based, so we will tell the program to print in plain text format. Then we tell it what to print.

We add:
print "Content-type: text/html\n\n";
print "This is our first perl program.";

Now we just save the file with out text editor as perlprogram.pl or perlprogram.cgi depending on how your web server is set up to manage perl files.

Next we upload the file to the cgi-bin on the server using any ftp or sftp program in ASCII or text mode. This is a text file and if uploaded in Binary mode, it wont work.

Next, we need to make the script executable on the server. Most FTP programs will allow you to set permissions of files. The program should be 755 or rwx-rx-rx.

To test the program just access the program with your browser by going to the url yourdomain.com/cgi-bin/perlprogram.cgi.

You should get a blank page with one sentence:

This is our first perl program.

And it is that easy to write and install a perl program.


Of course now you want the program to do more, so let us explore some basic functions and add them to our working program as we go.

Now that the program is installed, all we need to update the program is to upload the updated file to the server and overwrite the old file.


Our first task will be to use a varriable. Varriables in perl are designated by a dollar sign. The perl compiler will look for any word with a dollar sign in front of it and replace it with the assigned value. For our program we will use a dynamic value like the local time of the server.

First we need to define the varriable.

$ourfirstvarriable = localtime;

Then we need to add the varriable to our program. So far we have:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "This is our first perl program.";

Now we add in:

#!/usr/bin/perl
$ourfirstvarriable = localtime;
print "Content-type: text/html\n\n";
print "This is our first perl program. The time is $ourfirstvarriable";

Save the file, upload the program and look at it again with your browser. It should should show you the time on the server using the localtime format.

Notice that the varriable was inserted before the print command.

The perl compiler reads the program from top to bottom just like you would. So order is very important and you must define things before you actually use them. Just like writing directions to your house, you would not tell someone to turn right at the light but later tell them Oh, I meant the 3rd light. They are already driving down the wrong street.

Computers are not like people. They can't guess what you meant, they work from exactly what you write. So make sure your directions to the perl compiler are in order of how you want them followed.

>> NEXT