link to homepage
home site info services image licensing newest video prints

Examples of perl code

All the Perl that's Practical to Extract and Report

All of the pages at the domains and are generated by one or more perl programs. This page has some general information about how I've used perl to generate these pages. It describes the techniques I've come up with for generating a website from content stored in a database. There are additional comments on perl regular expressions.

Perl is an interpreted language (rather than a compiled language) so programs written in perl can be called "scripts". The perl language is well suited for processing text, accessing a database, and interacting with the internet. While I use Linux, perl is available on most other platforms.

The SQL Database

All the content for this site is stored in an SQL database. It has tables for "photos", "links", "comments", and "pages". Perl uses the information in the database to generate the pages on the website, injecting data into templates.


The database has templates with HTML for different kinds of pages. For example, the home page, thumbnail pages, and picture pages. The templates have tags to indicate where different elements should be inserted; I used the format "" to differentiate my tags from normal HTML.

The perl script retrieves information for a picture from the "photos" table, computes the internal links between the pages, inserts these elements into the template, and displays the page (this is done using the Fast CGI module)

The template is loaded into a scalar variable ("$template") and a substitution is performed for the "". All instances of in $template would be replaced with "2007-12-07", for example.

The perl code for this substitution looks like this:

$template =~ s//$date/g;
#This subroutine get_date returns
#the date in YYYY-MM-DD format
$tag{'date'} = &get_date;
@pids = &select_where("select id from photos where " .
	"deleted !='y' order by id");
#this subroutine returns the results of a
#MySQL select where statement as an array
sub select_where {
	my $string = shift;
	my (@row,@return) = ();
	my $sth = $dbh->prepare($string);
	$sth->execute || die $dbh->errstr;
	while(@row = $sth->fetchrow_array) { push(@return, @row) }
#this subroutine returns the current date,
#in the format "2000-01-01"
sub get_date {
	( $Second, $Minute, $Hour, $Day, $Month, $Year) = localtime(time);
	$Month = $Month + 1; $Year = $Year + 1900;
	if($Month !~ /\d\d/) { $Month = "0$Month" }
	if($Day !~ /\d\d/) { $Day = "0$Day" }
	$date = "$Year-$Month-$Day";

This page last modified on 2011-11-26

Post a comment on this page

phone at 612-245-4306   email us:chris\
Chris Gregerson, 150 Green Ave. N., New Richmond, WI 54017 USA

home   |   licensing information   |   site info
Chris Gregerson,