Building web sites with Perl

Perl can be use in a large number of ways. A very popular form is CGI (Common Gateway Interface) to create web sites. Using server side scripts you are able to create dynamic web sites that will be able to change the content according to the needs of the viewer or the people that own the web site. This web site you are now is dynamically generated using scripts and a database.

Most web server programs, including the free Apache server, can use Perl as a CGI for serving web pages. As a general practice the CGI is kept in a separate directory from the HTML files. Where your HTML files could be kept in /wwwroot/html/ your Perl scripts could be in /wwwroot/cgi-bin/.

This example uses the Pg library in Perl to access a PostgreSQL database. I would recommend you not use the Pg library but rather use the DBI library so you can access many more types of database server. Please see Database connections for more information.

/wwwroot/html/index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
    <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1">
    <TITLE></TITLE>
    <META NAME="GENERATOR" CONTENT="OpenOffice.org 641  (Linux)">
    <META NAME="CREATED" CONTENT="20020407;21274000">
    <META NAME="CHANGED" CONTENT="20020407;22153300">
</HEAD>
<BODY>
<!--#include virtual="/cgi-bin/Hello.cgi" -->
 
<FORM NAME="Standard" ACTION="/cgi-bin/Library.cgi" METHOD="POST">
 
<P>Show medias
<INPUT TYPE=submit NAME="Show" VALUE="Show"><BR></P>
 
</BODY>
</HTML>

/wwwroot/cgi-bin/Hello.cgi

#!/usr/bin/perl 
 
print "Content-Type: text/html\n\n";
 
print "This Perl CGI script will give you an example of using PostgreSQL with CGI.</P>\n";
exit;

/wwwroot/cgi-bin/Library.cgi

#!/usr/bin/perl 
 
use Pg;
use CGI qw(:standard);
 
$CgiQuery = new CGI;
$ShowButton = $CgiQuery->param('Show');
 
print "Content-Type: text/html\n\n";
 
if($ShowButton gt "")
{
   print "Show the contents of object_media<BR>\n";
   &ShowTable("object_media");
}
 
exit;
 
sub ShowTable
{
   local ($TableName) = @_;
   local ($SQL_Command);
   local ($NextIndex);
   local ($ALoopCounter,$BLoopCounter);
   local ($DBConnection);
 
   $DBConnection = Pg::connectdb("user=librarian dbname=library");
   if($DBConnection->status eq PGRES_CONNECTION_OK)
   {
      # Create the database
      $SQL_Command = "SELECT * FROM $TableName";
      $Result = $DBConnection->exec($SQL_Command);
      if($Result->resultStatus ne PGRES_COMMAND_OK)
      {      
         print "An error happend trying to open $TableName!<BR>\n";
      } else
      {   
         print "It worked!<BR>\n";
      }
   } else
   {
      print "Could not connect to database<BR>\n";
   }
}
 
sub MakeADatabase
{
   local ($SQL_Command);
   local ($Peramiters);
   local ($TheType, $TheOption, $TheCodes,$CodeCommand);
   local (@TheCode);
   local ($RealTable);
   local ($NextIndex);
 
   $Peramiters = "";
   $SQL_Command = "";
   # Look to see if they loged in ok
   $DBConnection = Pg::connectdb("user=postgres dbname=template1");
   if($DBConnection->status eq PGRES_CONNECTION_OK)
   {
      # Create the database
      $SQL_Command = "CREATE DATABASE mydata";
      $Result = $DBConnection->exec($SQL_Command);
      if($Result->resultStatus ne PGRES_COMMAND_OK)
      {      
         # &LogError("Error executing command = $SQL_Command");
      }
 
      # Create the user
      $SQL_Command = "CREATE USER myname WITH CREATEDB CREATEUSER";
      $Result = $DBConnection->exec($SQL_Command);
      if($Result->resultStatus ne PGRES_COMMAND_OK)
      {      
         # &LogError("Error executing command = $SQL_Command");
      }
 
      # Create tables
      $DBConnection = Pg::connectdb("user=myname dbname=mydata");
      if($DBConnection->status eq PGRES_CONNECTION_OK)
      {
         # Create and populate the template filling table
         # Create the table
         $SQL_Command = "CREATE TABLE myfriends (name CHAR(20), phone CHAR(20), email CHAR(50))";
         $Result = $DBConnection->exec($SQL_Command);
         if($Result->resultStatus ne PGRES_COMMAND_OK)
         {      
            # &LogError("Error executing command = $SQL_Command");
         }
 
         # Put some data in the table
         $SQL_Command = "INSERT INTO myfriends VALUES('Roy','(403)308-xxxx','roy@SiliconTao.com' )";
         $Result = $DBConnection->exec($SQL_Command);
         if($Result->resultStatus ne PGRES_COMMAND_OK)
         {      
            # &LogError("Error executing command = $SQL_Command");
         }         
         $SQL_Command = "INSERT INTO myfriends VALUES('Dave')";
         $Result = $DBConnection->exec($SQL_Command);
         if($Result->resultStatus ne PGRES_COMMAND_OK)
         {      
            # &LogError("Error executing command = $SQL_Command");
         }         
         $SQL_Command = "UPDATE myfriends SET phone = '320-xxxx',email = 'davet@someplace.net' $Peramiters";
         $Peramiters = "WHERE name = 'Dave'";
         $Result = $DBConnection->exec($SQL_Command);
         if($Result->resultStatus ne PGRES_COMMAND_OK)
         {      
            # &LogError("Error executing command = $SQL_Command");
         }                  
      }
   }
}
 
sub GetPhoneNumber
{
   local ($FriendName) = @_;
   local ($SQL_Command);
   local ($Peramiters);
   local ($TempReturn);
 
   $TempReturn = "";
   $Peramiters = "";
   $SQL_Command = "";
   # Look to see if they loged in ok
   $DBConnection = Pg::connectdb("user=myname dbname=mydata");
   if($DBConnection->status eq PGRES_CONNECTION_OK)
   {
      $SQL_Command = "SELECT phone FROM myfriends $Peramiters WHERE name = '$FriendName'";
      $TheSearchResult = $DBConnection->exec($SQL_Command);
      if($TheSearchResult->ntuples() > 0)
      {
         $TempReturn = $TheSearchResult->getvalue(0,0);
      }      
   } 
   return($TempReturn);
}
 
sub GetAllFriendsEmail
{
   local ($FriendName) = @_;
   local ($SQL_Command);
   local ($Peramiters);
   local ($TempReturn);
 
   $TempReturn = "";
   $Peramiters = "";
   $SQL_Command = "";
   # Look to see if they loged in ok
   $DBConnection = Pg::connectdb("user=myname dbname=mydata");
   if($DBConnection->status eq PGRES_CONNECTION_OK)
   {
      $SQL_Command = "SELECT * FROM myfriends $Peramiters WHERE name = '$FriendName'";
      $TheSearchResult = $DBConnection->exec($SQL_Command);
      if($TheSearchResult->ntuples() > 0)
      {
         $TempReturn = $TheSearchResult->getvalue(0,2);
      }      
   } 
   return($TempReturn);
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License