PerlHoo, Part I | 4 | WebReference

PerlHoo, Part I | 4

PerlHoo::Data Storage and Retrieval

Now that we have some idea of what PerlHoo is going to do and how it might work, it's time to decide how we're going to store and retrieve the data. We could store structured information in flat files, relational databases, XML, binary files, etc.. Before you break out your object modeller, re-read the requirements. Since we are desperate Perl hackers©, we only have one week to develop this application. We could use a relational database, but it would take too much time to install and to develop a data model. Alas, it would also take much time to write the code to manage a hierarchical database in almost any format.

Fortunately for us, the operating system provides a file system that manages our files for us in a hierarchical structure. There's no point in going through the trouble of reinventing the wheel (at least not today). So, we can create a directory structure that defines the Web directory hierarchy. Line 20 of the source defines the top level directory that will contain our Web directory hierarchy. Change this to whatever you like, so long as the directory is readable by the user that your Web server runs as. For categories that contain multiple words or spaces, replace the space with an underscore when you create the directory. PerlHoo will automagically translate all underscores to spaces when it displays the category name.

So we know how to define the Web directory hierarchy, but what about the data? That's where the Text::CSV module comes into the picture. Instead of writing an application that manages data input, we'll let the editors use their favorite spreadsheet application such as Microsoft Excel, Star Office, Applixware or any text editor. By saving the content in a comma delimited format, commonly called CSV in most spreadsheets, maintenance is a breeze and eliminates the code we would have written to maintain a database. The $datafile variable at Line 19 defines the name of this CSV file. For now, the file should contain 3 columns per entry:

  1. URL
  2. Resource Name
  3. Resource Description
For example, according to the values in our source, to add an entry for Mother of Perl to the Computers/Perl category, we would add the following line to /www/perl/tutorial/2/directory/Computers/Perl/perlhoo.csv:
"http://www.webreference.com/perl","Mother of Perl","Mother of Perl a free biweekly how-to devoted to all things Perl"

http://www.internet.com

Produced by Jonathan Eisenzopf and
Created: Mar. 18, 1999
Revised: Mar. 22, 1999

URL: http://www.webreference.com/perl/tutorial/2/