return to first page linux journal archive
keywordscontents

Listing 9. retrieve-today.pl

#!/usr/bin/perl -wT
# a CGI program that queries the database for the
# name of today's file, and prints that file to 
# STDOUT.
use strict;
use diagnostics;
use vars qw($docroot $query $dbh $sql $sth 
   $filename);
use CGI;
use DBI;
# Set this to the root directory for your Web 
# server
$docroot = "/usr/local/apache/share/htdocs";
# Create an instance of CGI
$query = new CGI;
# Send a MIME header
print $query->header("text/html");
# Open a connection to the database
$dbh = DBI->connect("DBI:mysql:test");
# Create the query, using MySQL's CURDATE() 
# function
$sql = "SELECT filename FROM Articles WHERE date = CURDATE()";
# Set up the SQL query
$sth = $dbh->prepare($sql);
# Send the query
$sth->execute;
# Grab the first returned row
($filename) = $sth->fetchrow_array;
# Open the file, and print it (or an error 
# message)
if (open (FILE, "$docroot/$filename"))
{
    print while (<FILE>);
}
else
{
    print "<P>No file for today.</P>\n";
}