Applying Perl Programming in Websites

Applying Perl Programming in Websites

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</h1>";

If this is saved to a file in the CGI directory (for instance, C:\Program Files\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, and the programmer will have taken their first steps to creating a complete web based Perl application.

No Comments

Post a Comment