1. Install and run apache on your system.
2. Install Perl on your system.
3. Write a "Hello World" program.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><head>\n";
print "<title>Hello, world!</title></head>\n";
print "<body>\n";
print "<h1>Hello, world!</h1>\n";
print "</body></html>\n";
4. Run the program
5. Explore The Possibilities
This advanced code should help you to start writing some of your own cgi scripts:
#!/usr/bin/perl –w
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use DBI; # we’ll talk about this in more detail later
my $mode = param(‘mode’);
my $url = url;
print header,
start_html;
if($mode eq ‘’) # two open quotes means ‘blank’ or ‘null’ or ‘empty’ in perl
{
print h2(‘Please enter your name’),
start_form(-action=>$url,
-method=>’post’),
hidden(-name=>’mode’,
-value=>’process_form’,
-override=>1),
textfield(‘name’),
end_form;
}
elsif($mode eq ‘process_form’)
{
my $dbh =
DBI->connect("dbi:$db_driver:database=$db_name;host=localhost;",
$db_username, $db_password, {RaiseError=>'1'});
my $sth = $dbh->prepare(‘insert into names (name) values (?)’);
$sth->execute(param(‘name’));
print redirect($url . ‘?mode=read_submitted_names’);
$dbh->disconnect;
}
elsif($mode eq ‘read_submitted_names’)
{
my $dbh =
DBI->connect("dbi:$db_driver:database=$db_name;host=localhost;",
$db_username, $db_password, {RaiseError=>'1'});
my $sth = $dbh->prepare(‘select * from names’);
$sth->execute;
while(my $row_href = $sth->fetchrow_hashref)
{
print $row_href->{‘name’} . br;
}
$dbh->disconnect;
}
else
{
print h2(‘Bad mode specified. Exiting…’);
}
print end_html;
There is a vast amount of information on Perl and CGI scripting out on the net. Some of our favorite sites are:
Click here to download our presentation from class