Link to Home Page Link to Books and Advice Link to Internet Resources Link to Problem Solving Link to Feedback

Perl/CGI Tutorial

Writing a Perl script to print a line of text is very easy. The "Hello, world!" script that's mentioned in the 1st edition of Learning Perl, by Randal Schwartz goes like this:

#!/usr/bin/perl
print "Hello, world!\n";

Pretty easy. But how do I get this to run on the Web, you ask? There are a few more variables (no pun intended) that come into play and I will try and get you to that point with a minimum of hassle. This assumes you already know HTML, how to upload the files and how to issue commands using Telnet. If you don't yet, I would suggest searching for HTML tutorials on the Web and going from there.

Here is the program right off the bat:

#!/usr/bin/perl

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

print "<html><head>\n";
print "<title>Hello, world!</title></head>\n";
print "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#FF0000\" vlink=\"#800000\">\n";
print "<h1>Hello, world!</h1>\n";
print "<a href=\"../perl/tutorial.html\"><b>Back</b></a> to the Tutorial\n";
print "</body></html>\n";

So what do we have here? The first line is the standard reference to the location of Perl on your server. If you're not sure where Perl is on your server, type

which perl

at the command prompt on a Unix server and it should tell you. If this doesn't help you, talk to your system administrator.

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

The next item is the HTTP header, which is the Perl script telling the browser what type of document it will be displaying after the server executes the script. It's telling the browser that it will be displaying a text (which is what HTML is) HTML document, as opposed to a plain text document, image or any other number of content types. This line must be included before any of the print statements in the script. This line must be followed by a blank line which is why there are two new line commands (\n) included. The first one goes to the next line, the second puts a blank line after it.

After the browser knows what type of stuff it will be displaying, the script gets around to printing the HTML tags. Here we have simple print statements, enclosed in quotes with a new line character and the semicolon to end the statement. The new line character isn't necessary, but it makes the code displayed by the browser easier to read. Otherwise when you View Source using your browser, the HTML would be on one long line.

print "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#FF0000\" vlink=\"#800000\">\n";

I included a body tag with colors in it for a reason. The double quotation mark inside the body tag would normally confuse the Perl program because double quotation marks are used to contain a statement or string. So the double quotation marks need to be escaped with a backslash so that the Perl program won't see them as it would normally see a double quotation mark, which would mark the beginning or the end of a statement in this case. In a double-quoted string, there are four characters that have special meaning: $, @, \, and the quote " itself. These need to be escaped (protected) with a backslash if you want to prevent the special meaning from taking effect. For example:

"The four characters \" \$ \@ and \\ are special here."
Remember this when you write your CGI scripts, especially the @ character in an e-mail address within a double quoted string. You will avoid many headaches by remembering this.

Type this script using any text editor and save it using whatever name you want with a .pl extension. (Don't try copying and pasting from the browser. It may not translate correctly and you will learn better by typing it in.) Upload it to your server's cgi-bin directory. (If you are in the Windows environment, be sure to upload the file in ASCII format, not Binary.) Be sure to give it the correct permissionss by typing

chmod 755 whatever.pl

at the Telnet command line where whatever is the filename you gave it. If you don't have a cgi-bin directory but your server allows CGI programs, you may need to name it with a .cgi extension and give it the proper permissions in whatever directory it's in. If you need more info on this, go to the Problem Solving page and read more about it. (You will want to learn more about this anyway.)

Now you can access the script by typing in the URL with the filename appended like this:
http://www.webdesigns1.com/cgi-bin/hello.pl
If it doesn't run for you the first time you try, don't be dismayed, this is how you learn! (And learning on the computer always seems to involve some pain.) Be sure you have every character in exactly the right place and that you have the correct file permissions. If it still doesn't work, run the script from the Telnet command line by typing

perl whatever.pl

and see what it tells you. If you still can't get it to work, just ask here or on the CGI newsgroup as mentioned on the Internet Resources page. Once you get your first script to run, the trouble will have been worth it.

That will hopefully get you on your way. From here you can move on to a wealth of books and tutorials that can be found from within this site.

Note-I wrote this tutorial because I wanted to contribute something other than just lists of resources. I'm not a professional writer and I'm not an expert with Perl. If you have any suggestions as to how to improve this tutorial, please use the feedback form.

Link to Home Page Link to Books and Advice Link to Internet Resources Link to Problem Solving Link to Feedback

Nowadays website is considered the best part time earning source. People are making good earnings from their websites as it does not need any big investment but the return on it is exceptional. All you need is good skill and working will. As a starting step you need domain registration. You can have domain registration from your ISP. Then you need web site hosting. If you have a big website with huge traffic then you will need dedicated hosting. Most of the hosting companies offer their dedicated servers with good features including web design and internet marketing. You can use their web design software to build some dynamic web pages. Service of affiliate marketing provided by your host can also prove helpful to improve the rankings of your website. For all this you need a good internet connection which can be obtained from any ISP.