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:

 

mysqltemp

 

 

 

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]);

Roulette en ligne en ligne : mise simple, mise double (a cheval), mise triple (transversale), mise en coin, mise sur six lignes, mise « panier », mise sur la ligne superieure, mise sur 1 a 18, mise sur 19 a 36, mise sur le noir ou le rouge, mise sur pair ou impair, mise sur une douzaine, mise sur une colonne et mise en zigzag. 0in 0pt;”>

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:


mysqltemp

 

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;

Be the First to Comment!