Modules exist for almost everything, such as database connections, HTML parsing (HTML.pm, LWP.pm), operating system specific features, networking, mail, encryption, language processing and graphics. Most of the modules use object-oriented Perl (see week 12).
Database interfaces support the following tasks:
Below are examples for accessing a Mysql database (without or with HTML Templates) and for Postgres. There are no exercises for this section because not all students have database accounts.
#!/usr/local/bin/perl -w
########################
use CGI qw(:standard -debug);
use Mysql;
use DBI;
########################
print header();
print <<EOS;
<html>
<head> <title>DB</title>
</head> <body>
EOS
$db = DBI->connect('dbi:mysql:yourdb;host=zeus.napier.ac.uk',
'u123', 'password') || die "Cannot connect to database";
$query = $db->prepare( qq { SELECT * from this_table;} );
$query->execute();
print "<table border=1> <tr>";
foreach $i (@{$query->{NAME}}) {
print "<th> $i ";
}
while (@row = $query->fetchrow()) {
print "<tr>";
foreach $col (@row) {
if ($col eq "") { $col = ' '; }
print " <td>$col ";
}
print "<br>\n";
}
$db->disconnect;
#!/usr/local/bin/perl -Tw
####################################################################
use CGI qw(:standard -debug);
use HTML::Template;
use Mysql;
use DBI;
####################################################################
my $template_text = <<EOS;
<html>
<head><title> Database</title>
</head><body>
The Database results are:<p>
<table border=1>
<tr><th>Name <th>Job
<TMPL_LOOP NAME="EMPLOYEE_DATA">
<tr><td><TMPL_VAR NAME="name"> <br>
<td><TMPL_VAR NAME="job"> <p>
</TMPL_LOOP>
</table>
EOS
################## CGI and Template start #########################
my $cgi = CGI->new();
my $template = HTML::Template->new(scalarref => \$template_text );
print $cgi->header();
###################### Database commands ###########################
$db = DBI->connect('dbi:mysql:yourdb;host=zeus.napier.ac.uk',
'u123', 'password') || die "Cannot connect to database";
$query = $db->prepare( qq { SELECT * from this_table;} );
$query->execute();
while ($row = $query->fetchrow_hashref()) {
push(@loop_data, $row);
}
$db->disconnect;
################## Template is printed to browser ################
$template->param(EMPLOYEE_DATA => \@loop_data);
print $template->output();
(If you want to install a free SVG viewer on your home computer you can find one on this page. As far as I know, both the Adobe Viewer and the Java-based Batik/Squiggle SVG browser are reasonably good at displaying SVGs. I have heard rumours about the Adobe viewer causing problems with IE or the windows registry. Not sure if that's true - but you might want to read about possible problems on the WWW before installing it.)
The following example reads a color parameter from a form and then paints a circle in that color.
#!/usr/local/bin/perl -w
use SVG;
use CGI qw(:standard -debug);
my $color = param("color");
# the next line should be uncommented for some security checking
# if ($color !~ /^\w+$/) {die;}
# create an SVG object
my $svg= SVG->new(width=>200,height=>200);
# draw a circle at position (100,100) with ID 'this_circle'
# which is filled in $color
$svg->circle(id=>'this_circle',cx=>100,cy=>100,r=>50,
style =>{fill =>"$color"});
# create and print the SVG code
print "Content-type: image/svg+xml\n\n";
my $out = $svg->xmlify(-dtd =>
'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd');
print $out;
"Information architecture is the art and science of structuring and organising information environments to help people achieve their goals" (Argus, 2000). Information architecture attempts to balance the technical requirements with user requirements and information system requirements. Some examples where information architecture can help are: