Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum Rss Feed

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
Free Newsletters:

News Via RSS Feed

Rss Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles

SQL etc

Posted Aug 30, 1999

Using a flat-file database in Perl - Page 4

By Jason Shindler

The next lines print the HTML output, part 1. In this area, you'll want to put any information that is the same for every page the program prints. A standard menu that runs atop the page is an example of something that might go in here. In this example, this includes HTML headers, theater information (which is reliant on the information gleaned from the text file on the previous page), and the top part of the showtimes table. The information from the text files is used in the HTML simply by typing the variable's name. For instance, to use the $showthisdate variable we created before, simply type "$showthisdate" in your HTML and Perl will insert the right information. For brevity, this part of the example is in a text file.

Note: This ability to use variables inside the HTML has one noticable disadvantage: Anytime you use a character in your HTML that is also used in Perl to represent varaibles, you must add a backslash. For Instance: to write $3.83 in your HTML, you must write \$3.83 instead. This must also be done for @, as "at" is used for other Perl variables.

This next segment imports the studio information from a file, just as we did for the theater information before.

open(STUDIO, "studio.data") 
   or die "Domain.com could not open the Studio 
           Information file.\n
           Please email webmaster at domain.com 
           with the following information:\n$!\n";

while ($line = <STUDIO>)
   ($studioname[$s], $studio[$s], $studiourl[$s])
    = split /;/, $line;

Now the showtimes data file is more complicated. We input the information as before. But, as we cycle through each line of the file, we also set some other variables for later use when writing each line of the table.

open(SHOW, "showtimes.data") 
   or die "Seethemovies.com could not open the main 
           Showtimes file.\nPlease email webmaster 
           at domain.com with the following 
while ($line = <SHOW>)
   ($bdate[$b], $edate[$b], $m[$b], $digsound[$b],
    $showtimesm8[$b], $showtimesm400[$b]) = split /;/, $line;
   if ($showtimesm8[$b] != "") {
      $movies8[$b] = 1; }
   else {
      $movies8[$b] = 0; }
   if ($showtimesm400[$b] != "") {
      $movies400[$b] = 1; }
   else {
      $movies400[$b] = 0; }
   if ($theater eq 2) {
      $movies[$b] = $movies400[$b];
      $showtimes[$b] = $showtimesm400[$b];
   if ($theater eq 1) {
      $movies[$b] = $movies8[$b];
      $showtimes[$b] = $showtimesm8[$b];

Next, we cycle through each value in the array of showtimes and check to see if it meets the characteristics we're looking for. In this example, we check if the showtime is for the right theater and is valid for the day we're searching for. It then displays the movie and showtime specifics for that particular showtime, using the same technique used above (recall: to use information from our text files, simply write the variable's name inside the HTML).For instance, a user might ask for January 25. If the date range is between January 1 and January 10, it won't display anything. If it's between January 20 and January 30, it will. The loop in this section of code terminates when $c >= $b; in other words, when each line that was imported into from the text file has been processed. Note: The use of "$pic[$m[$c]]" may look confusing (it is supposed to return the picture that corresponds to the movie specified on the line of showtimes the script is processing -- so it also sounds confusing). But by thinking logically, it is easy to see what's happening. Perl evaluates $m[$c] first, and returns a number (which is the number of the movie), then Perl evaluates the number like this "$pic[<NUMBER>], thus returning the correct picture.

# $c is incremented until the last showtimes.data
# line has been processed
while ($c < $b ) {
   if ($movies[$c] eq "1" && 
      ($bdate[$c] <= $datetosearch) && 
      ($edate[$c] >= $datetosearch) {
      print <<EOF ;
       <TD WIDTH="20%">
          <A HREF="http://domain.com/cgi-
        <CENTER><IMG SRC="
        <CENTER><B><FONT COLOR="#800080">$digsound[$c]</FONT></B></CENTER>
       <TD WIDTH="20%">
        <CENTER><FONT SIZE=+1>
       <TD WIDTH="20%">
       <TD WIDTH="20%">
        <CENTER><a href=$studiourl[$s[$m[$c]]]><IMG SRC="

Simple stuff again at the bottom. Insert any HTML footers or menus that run at the bottom. Take note of the \@ used instead of @ when displaying an email address in the HTML. As noted earlier, this avoids confusing between an email address and variables that start with @ in Perl. Note: If you do not do this, you WILL get an "Internal Server Error".

print <<EOF ;
<p><b><font size=4><center>
SIZE=+1>Copyright 1998&nbsp;</FONT></FONT></B>
SIZE=+1><A HREF="mailto:norman\@domain.com">MnM
Entertainment</A> &amp;</FONT></FONT></B>
SIZE=+1><A HREF="mailto:webmaster\@domain.com">Jason

That's it! Your script is finished :)

From here....

You've mastered some basic Perl and hopefully designed a script that meets your needs. We've learned how to read from files, from here it might be useful to write to files as well. To take this example, one could write additions to this script that allow users to write comments about each movie and rate the movie on a scale of one to ten.

This method also has several other applications. Among them, you read from text files to put a similar banner or navigation menu on the top and bottom of every page. Simply ask Perl to read in a file that contains your menu, read in another file that contains the data for a specific document and concatenate them together using the familiar Print statement. The opportunities are endless. Happy coding :)

SQL etc Archives

Latest Forum Threads
SQL etc Forum
Topic By Replies Updated
MySQL rollback UAL225 0 August 21st, 09:56 PM
Complex Search Query Galway 0 May 20th, 10:04 PM
change collation at once supercain 2 May 15th, 06:18 AM
SQL Features, tools and utilities question Neomite 1 April 10th, 09:13 AM