| Article Index |
|---|
| Programming an Intranet with Apache, MySQL, and Perl |
| Checking you system |
| Creating a first webpage with Perl |
| All Pages |
Programming an Intranet with Apache, MySQL, and Perl
Why Perl?
There is a time when our Perl programs need to go out of only command line. Don’t get me wrong, there is no problem with command line, but some people find that it is no so friendly, ugly or whatever
reason.
One day your boss will come to you and say: “I want you to make all application you made that gather information from the server available for our customer support level 1”
And then you say: “They are already available!”
You boss angry responds: “People from support level 1 are not familiar with command line! I want something graphical, friendly and accessible from any computer in the company.”
Everything can be done with Perl, we can create modules to administer servers, web interface, and also we can make Apache modules. What is even better is the fact that we won’t spend a buck with software licensing and we don’t need to install anything new on the Linux servers.
Besides all the others functionalities Perl is capable of administer web applications. In order to do that we have CGI, DBI, Catalyst and much more that make easier the life of a programmer.
This article will show an introduction to CGI application with Apache, Perl, and MySQL. It is possible to implement Perl on Windows server (IIS) but I this is beyond of the scope of this article. You can find some information about it at ActivePerl | ActiveState Community Site.
Checking you system
Obviously you will need a Linux system with Perl, MySQL and Apache installed. For now in my case I am using the default configuration made by the Debian distribution installation.
On the apache configuration you need to make sure that you add the following line in the configuration.
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "<PATH of your CGI directory>"
In order to check mysql you can use the command line myadmin, the QueryBrownser. The version we are going to use is 5.0.51a

Perl can be the ActiveState one or that one from the original installation. Only make sure that it is updated. Any version higher than 5.6 is fine.
Type:

Now everything is checked, let go and have some action.
Creating a first webpage with Perl
Open a text editor and type the following code and save as “environment.pl” in the directory where it is defined in the field ScriptAlias of your apache configuration.
Usually it is a directory named cgi-bin. In my case it is at /usr/lib/cgi-bin.
#!/usr/bin/perl
use warnings;
use strict;
use CGI; #load cgi module, which obviously let us create a cgi script
my $cgi = CGI->new; #Instantiate a CGI class
print $cgi->header, # First we create a header
$cgi->start_html('My first Perl website'), # Begin HTML page
$cgi->h1('Welcome to Perl'), # create the Tag <h1>TEXT</h1>
$cgi->p(scalar localtime), # create <p>Text</p>
$cgi->h3("My local variables:\n");
foreach my $myKey (keys(%ENV)) {
print $cgi->p($myKey . " => $ENV{$myKey}");
}
print $cgi->end_html; # end of HTML
In order to run this Perl filewe give the environment.pl file executable permissions:
chmod +x environment.pl
So the result on your browser should be something like that:

CGI module is a very complete and mature Perl solution which can receive and create http requests. This is a solution with more than 10 years of developing. I started with CGI on this series of articles because it is recognized one of the main Perl modules.
With CGI we can create tags HTML using object oriented programing style.
$myObjetCGI = CGI->new;
$myObjetCGI->tagHTML([parameters]);
A good thing about writing codes this way is the organization. The code will be shorter and clear than the traditional HTML/XHTML. Another good thing is that you can create dynamic pages easier and the biggest advantage is that you can take the power of Perl to your web applications.
In order to see a practical feature of this power, go on your command line and run your script and take a look at the output:

Notice that the first line we invoke strict and warning. Always use ‘strict’ and ‘warnig’ on your scripts; this will help you debugging your file. After that we load the CGI module and we create a list of instances of the CGI module itself:
$my $cgi = CGI->new
Using the object oriented mode is best practice for complex web applications that need many modules. If you use some as objects e others as functions loaded your code will be no clear to understand.
This part:
print $cgi->header, # First we create a header
$cgi->start_html('My first Perl website'), # Begin HTML page
$cgi->h1('Welcome to Perl'), # create the Tag <h1>TEXT</h1>
$cgi->p(scalar localtime), # create <p>Text</p>
$cgi->h3("My local variables:\n");
We are creating a HTML heading and we create a body of the page. The function ‘localtime’ returns a array with the local time.
This part:
foreach my $myKey (keys(%ENV)) {
print $cgi->p($myKey . " => $ENV{$myKey}");
}
A hash is a type of Perl variable that holds values related to a key.
The hash %ENV is a Perl internal variable and loads the environmental variables on a format.
%ENV = {Variable => variable_value}.The function ‘keys’ retunrs a array of all the hash keys. So in our case it will return the name of all variables and for each of them – ‘foreach’ – it will show on the html file the name_of_variable => name_of_variable.
And we end this with:
print $cgi->end_html;
Last Updated (Saturday, 08 January 2011 03:57)



Comments
RSS feed for comments to this post.