How to Use Perl Programming on a Web Site

Starting CGI Programming with Perl

© Mark Alexander Bain

Jul 19, 2009
How to Use Perl Programming on a Web Site, Perl Foundation
Per is one of the easiest programming lanuages to start with when it comes to CGI application development - it doesn't even mattter if the web server is Linux or Windows.

Perl is a powerful and versatile scripting language for both Windows and Linux. Within a few minutes any programmer can develop a script to carry out their desired operations, and this is made even easier when one of the myriad of available modules are used. This is particularly true of web site development.

All that a Perl programmer has to do in order to become a web site developer is to download and install the free Apache HTTP web server. Once this has been installed (and the installation process is very simple) then the programmer is ready to start programming with Perl.

There are only three things that a Perl programmer needs to be aware of when they start programming on a web site:

  • typically the Perl script will need to be placed in the web server's CGI (Common Gateway Interface) directory
  • the script file must contain a shebang line
  • the script will need to contain a HTTP header line

These may sound complicated to the uninitiated, but are actually very easy to work with.

The Perl Shebang Line

The very first line of any Perl Script to be used on a web server must be the shebang line. This always starts with a "#!" prefix and simply tells the server where to find the Perl executable. Therefore if Strawberry Perl were to be used on a Windows computer then the shebang line would be something like:

#!c:/strawberry/perl/bin/perl

Apache will now use this executable to process the Perl code created by the programmer.

The Perl HTTP Header Line

The second line of any Perl script for the Internet then a HTTP header has to be generated, and this includes vital information about the document. The Perl programmer does not need to know anything about the header. All that they need to know is that the second line of their script (after the shebang line) must be:

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

Any output from the script will now be processed correctly by the browser.

Producing HTML Output with Perl

The Perl programmer will, of course, want to produce information that will be displayed in a their users' web browsers. That's done by printing standard HTML:

print "<h1>Hello World</h2>";

If this is saved to a file in the CGI directory (for example, C:\Program Files\Apache Group\Apache\cgi-bin\helloworld.pl) then it can be access in a web browser by using the URL:

http://localhost/cgi-bin/helloworld.pl

If the shebang and HTTP header lines have been added correctly then the user will see some text displayed in their web browser (as shown in figure 1 at the bottom of this article), and the programmer will have taken their first steps to creating a complete web based Perl application.


The copyright of the article How to Use Perl Programming on a Web Site in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish How to Use Perl Programming on a Web Site in print or online must be granted by the author in writing.


How to Use Perl Programming on a Web Site, Perl Foundation
Figure 1: A Simple Perl Web Page, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Nov 7, 2009 4:39 AM
Guest :
Thanks, for give your valuable time to write this whole tutorial. This tutorial give me all the steps to use PERL over server.
Best Regards
Mohit Jindal
1 Comment: