First Perl Script
Posted in Perl | Posted on 25-09-2009-05-2008
In this tutorial, we will be introduced to a scripting language called Perl. Perl is a Server-Side scripting language; meaning that the script will run off of a server.
Scripting languages give us the opportunities to create more dynamic websites; whether it be extracting data from a database, accepting input from an HTML form, or even image manipulation such as rotating banner ads. Scripting provides us with possibilities that HTML by itself does not offer.
One small draw back about scripting languages is that the learning curve is a little more steep as opposed to learning HTML code.
In order to experiment with a Perl script, you will most likely need to have access to a server that allows you to run Perl or cgi scripts. If you are a Windows user, you can also download ActivePerl. ActivePerl will just display the output of the script in text. The server will display the output of the script as a web page.
If you have access to a web server, check for a folder called the cgi-bin. If you find this folder, you should be good to go.
Let’s work on this example:
#!/usr/bin/perl -wT
use CGI qw(:standard);
use strict;
print header;
print <<HTMLOutput;
<html>
<head>
<title>My first Perl Script</title>
</head>
<body>
<h1>This is my first Perl Script</h1>
</body>
</html>
HTMLOutput
On the last line of code (HTMLOutput), make sure to hit enter. You need to have a blank space below this line of code or else the script won’t work. If you are a little confused about this, just put the mouse cursor on the right side of the lowercase t and simply press enter.
Save this file as:
first-perl-script.pl or first-perl-script.cgi
Whichever of the two you name it will be fine. Next, log in to you server and put this file in the cgi-bin.
This next step is important. On the server, make sure to change the file permissions to 755.
It’s time to run our script. Here’s the link to see what mine looks like:
http://www.learnhtmlonline.com/cgi-bin/first-perl-script.pl
If you get an Internal Server Error page, make sure that you changed the permissions to 755, and that you added that extra blank line at the end of the actual script. With just one syntax error, your script will not work.
If you got your script to work, you can see that we pretty much have a plain HTML page. You might be thinking what’s the point of all this? Remember we are just getting our feet wet with Perl. But as you get better with Perl or any other scripting language, you will discover that you can accept user input and manipulate the data the way you want. In a nutshell, this is powerful stuff. Think about when you log in to your email account or when you make a purchase online. You have just used a server side script.
Understanding the Code
Without boring you, we won’t go into complex details about some of the code.
#!/usr/bin/perl -wT
use CGI qw(:standard);
use strict;
The first line of code tells where the Perl interpreter is located on the server. The second line of code is a Perl module that allows us to perform many programming tasks in Perl. The third line is really not necessary for this script, but it good programming style if you use it. use strict; requires us to declare all variables in the script. In this script there are no variables. (Variables are values that may or may not change throughout the script.)
This line of code is important for this particular script or any other script where the output will be a web page. It is equivalent to: Content-type: text/html\n\n;. What that means is that the output of this script will be an HTML page. By using the CGI module, we can use print header; instead to save us some typing time and to keep our code looking clean and easy to read.
print <<HTMLOutput;
<html>
<head>
<title>My first Perl Script</title>
</head>
<body>
<h1>This is my first Perl Script</h1>
</body>
</html>
HTMLOutput
These lines of code contains our HTML output. Since we are using the CGI module, we use print <<HTMLOutput; and end it with HTMLOutput. Don’t forget to add that extra space below it or else your script won’t work. Keep in mind, you can name this anything you want. It doesn’t have to be HTMLOutput. That is just what I named it. Just make sure that the beginning and ending of the output are of the same name.
Final Word
This was a really simple script that we worked on. To become good at a programming language you must practice, practice, practice; not unless you are just a natural coder. Try to understand how the whole concept of server side scripting works. In time you will get better.
