CGI and Perl- The Basics
Posted On January 21, 2008 by Sneha Latha filed under Internet
I will suggest you to checkout the simple tutorials we had presented during the month of July to get a background of Perl, if you do not have one. We had attempted discussing about Perl from the perspective of any other language programmer. In this month, we will explore Perl from a more practical manner.
Today Perl finds use in two areas
1) Perl is still the most preferred way to plumb the Internet Infrastructure. That means, it is still one of the most popular languages to create dynamic web pages on the net.
2) It makes great sense to use Perl in some areas such as Bio-informatics where you need to do pattern matching and analysis. Perl supports Regular Expressions, which is extremely useful for such work.
In this series of articles I will give you a gentle introduction to programming with Perl for the Internet. If you are a master with Perl, stay away, there may be other pages in this magazine which will be of interest to you.
Now if I were to take a logical approach, I should have discussed some more topics such as scalar arrays and functions before I delve into the world of Internet programming. But I will put logic into the backburner and discuss art of Internet programming.
The concept of CGI
| China jumps on Linux trainMembers of Open Source Development Labs (OSDL) -- home to Linux founder Linus Torvalds -- announced during first week of August the establishment of its first office in China in the city of Beijing. The goal is use the office as a hub in the Asia-Pacific region to further Linux adoption and advocacy. Beaverton, Ore.-based OSDL also has facilities elsewhere in the U.S and Japan. The effort has the backing of the Chinese Ministry of Information Industry (MII) and the Ministry of Science and Technology (MOST). "China is a leader in embracing Linux," said Stuart Cohen, CEO of OSDL in a statement. "The government is promoting Linux, a vibrant local software industry is blossoming around Linux, and the world's leading IT firms are investing heavily to deliver Linux-based computing solutions to Chinese customers. OSDL is committed to helping all of these groups to advance Linux in China. " Not that it comes as a surprise to anyone in the Linux community, but analyst firm IDC forecasts the market for Linux in China is growing at a faster rate than any other operating system including Microsoft Windows. "Although the Linux OS market only represented a small portion of the total operating system and subsystem market in China, Linux OS will maintain above-average growth compared to the overall OS growth rate in China," Jenny Jin, software analyst for IDC China wrote in a July 2004 report titled, "China Linux 2004-2008 Forecast and Analysis." "IDC predicts that the development of Linux in China will have strong growth starting in 2004. The Linux market will continue to grow at a faster rate than the rest of operating environments industry, including Windows, for the next few years," the IDC analyst wrote. Statistics cited by the OSDL from Chinese IT research firm CCID Consulting show that the compound annual growth rate (CAGR) for the Linux server software market will grow to 49.3 percent over the next five years. Linux vendor, Turbolinux claimed in a recent press release that it is the primary distributor of Linux server operating systems in China based on IDC statistics. The Japanese-based software vendor said in 2003 Turbolinux held a 60.8 percent share of Linux server revenue and a 60.7 percent of Linux OS shipments. Nearly 40 percent of Turbolinux's direct sales shipments were made to the government of the People's Republic of China. In June, Turbolinux inked a deal with the China Ministry of Railways to deploy its Linux OS. |
In fact I gave a background of CGI in the very first article in the series in July 2004.The most important concept is the CGI programming concept when it comes to Internet programming with Perl. Essentially the Common Gateway Interface programming involves designing and writing programs that receive their starting commands from a Web page. Such web pages use an HTML form to initiate the CGI program. The HTML form has become the method of choice for sending data across the Net because of the ease of setting up a user interface using the HTML Form and Input tags. With the HTML form, you can set up input windows, pull-down menus, checkboxes, radio buttons, and more with very little effort. In addition, the data from all these data-entry methods is formatted automatically. This is what is called plumbing the Internet infrastructure at least for a small web site owner and Perl fits the bill to the hilt.
Now it is important to understand the underlying relations between HTTP, HTML and CGI to understand the way HTML programs work.
HTML is fundamentally a method to format text. At least that is what it is in its raw sense. It can help you to have larger or smaller fonts, change color of your fonts, and finally hyperlink areas within the page to link
HTTP is the transport mechanism for sending data between your CGI program and the user. This is the behind-the-scenes director that translates and sends information between your Web client and your CGI program.
Your CGI program is responsible for understanding both the HTTP directions and the user requests. The CGI program takes the requests from the user and sends back valid and useful responses to the Web client who is clicking away on your HTML Web page.
Hence it is a three stage play involving three players. Understanding this aspect is very important and hence I will use another crude but hopefully effective illustration.
Consider that you are posting a letter, and a post man delivers the letter to your friend and he replies back which is again carried by the post man back to you. Similarly HTML provides you with initial information to be displayed (writing the letter), HTTP transports the information to the server (post man delivering the letter), and is replied back (CGI program working) to you.
Hence HTML carries the instructions to the CGI program, which executes a program on the server. The data gathered by the browser gets to your CGI program through the magic of the HTTP request header. The HTML tags tell the browser what type of HTTP header to use to talk to the server through your CGI program. The basic HTTP headers for beginning communication with your CGI program are Get and Post.
Your CGI program must be aware of the HTTP request headers coming in and its responsibility to send HTTP response headers back out. Beyond that, your CGI program can do anything and work in any manner you choose.
I will not delve into the details of how exactly all these work from the guts of the system. That calls for a detailed analysis, which we will do it later. For all this you need to set up a working system, which will help you program CGI.
Let us try configuring Apache. A Web Server is required to run your CGI scripts, and since Apache is free, it is the best way to start off. Also 99% of UNIX servers run on Apache.
Installing Apache on Windows
In this tutorial I am installing Apache from scratch on my Windows 98 desktop to run the simple CGI scripts. Download and install Apache from the CD. The file is likely to be apache_2.0.50-win32-x86-no_ssl.msi.
When you install Apache, you'll get a prompt for "Server Information." Here is the settings which is preferred for this tutorial.
Network Domain: localhost
Server Name: localhost
Admin Email: mymail@mailserver.com (you use your mail id)
You will then be prompted to run Apache as a service or manual. I will recommend you to run it as a service. It works fine on all flavours of Windows I had checked on.
It is advisable to install Apache on a root drive like C:/ or E:/ rather than the option, which is given by the installer. This is because you will find it easier to work from the first level directories.
Once the installation is over, automatically Apache is started. You will also find that Apache sub menu is installed in the Programs.
You can checkout whether everything is fine with the installation by typing in http://localhost, then you should get a web pages from Apache. Alternately try http://127.0.0.1. If you do not get a web page with an Apache logo then there is some trouble, which needs to be fixed.
One of the usual problems is that in case you have IIS Server on your PC, then IIS will perhaps be invoked first. Hence you may have to disable IIS Server.
After that you install the Active Perl MSI file on to the system. Since you are likely to use your Perl scripts on a UNIX Server, it is advisable to maintain compatibility. So create a directory called C:/usr or E:/usr as the case may be and let Perl be installed there.
For example, on my Linux machine, Perl is located at /usr/bin/perl and so every Perl program that I write begins with #!/usr/bin/perl So, when I install Perl on an Windows machine, instead of installing it in the default location (which is C:\perl for ActivePerl) I install it in C:\usr so that the Perl executable is located at /usr/bin/perl. This allows me to write code on my Windows machine, and then move it, without changes, to my Linux machine, and have it run there.
Using Notepad (or other text editor) open httpd.conf (and search for Options Indexes FollowSymLinks (about line 267) when you find it add ExecCGI to the end so it looks like Options Indexes FollowSymLinks ExecCGI. This will activate cgi programming on your server or PC.
Now run a program called test.cgi.
#!/usr/bin/perl
print "Content-type:text/html\n\n";
print "I am planning to learn CGI";
You execute the script from your web server on either from http://localhost/cgi-bin/test.cgi or from http://127.0.0.1/cgi-bin/test.cgi
You will get an output as expected on your web browser. Make sure your Apache server is running!
If it renders the message that “I am planning to learn CGI” on your web browser you know that your program is working.
Congratulations! Your first CGI program is running!
One of the biggest advantages of CGI is the security it provides. This is something that is missing in ordinary HTML. Your HTML code is always exposed, once you check through View à Source option in your web browser.
Trying checking for the Source, you will see that the CGI program does not display the source code.
As I mentioned before you will be running CGI scripts from UNIX servers most of the time. Does it mean that you need to configure a Linux server? It is good if you can. But not necessary if you are a newbie programmer.
There are number of free web servers available that support CGI. So you can actually test your code for Linux on these web servers.
I will suggest you to checkout free.prohosting.com, however other options such as tripod.com and freewebpages.com exist. But I have not checked the services of the last two mentioned ISPs.
Its free and supports up to 50 MB space, and enough space for you to learn CGI programming! You can create an account in less than ten minutes just the way I did.
Then you can use a free FTP client and upload the test.cgi onto the cgi-bin directory on your web space. Check this out on http://storm.prohosting.com/webzary/test.cgi
Before you upload the files onto the free server. It is very important for you to understand the different permissions, which you need to set for a Linux server.
Unlike Windows UNIX provides number of detailed protection options for different files
UNIX will only execute programs which are executable or which UNIX recognizes to be the executable. Even if everything else about the program is right, if you don't tell the server that your program is executable, it will never work! You might know that it's a program.
If you are using Linux or any other forms of UNIX then to make a program executable you will use this command
-rw-rw-rw- program.name
See the table on chmod options to understand more about file permissions in UNIX.
Most of the FTP programs actually let you set file permissions even from Windows clients. Hence using this option your can set the test.cgi’s file permissions to 755 from the Windows client.
This ensures that your Windows programs work smoothly on your Linux server! We will delver more into CGI Perl programming in the next article.
