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

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
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";
$s=1;

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

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 
           information:\n$!\n";
$b=1;
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];
   }
   $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
$c=1;
while ($c < $b ) {
   if ($movies[$c] eq "1" && 
      ($bdate[$c] <= $datetosearch) && 
      ($edate[$c] >= $datetosearch) {
      print <<EOF ;
      <TR>
       <TD WIDTH="20%">
        <CENTER>
         <H2>
          <A HREF="http://domain.com/cgi-
bin/moviewizard.cgi?movienum=$m[$c]&showdate=$datetosearch">
$name[$m[$c]]</A><IMG
SRC="http://domain.com/images/ratings/$rated[$m[$c]].gif" 
NOSAVE BORDER=0 HEIGHT=35 WIDTH=47 ALIGN=ABSCENTER></H2>
        </CENTER>
        <CENTER><IMG SRC="
http://domain.com/images/movies/$pic[$m[$c]]">
        </CENTER>
        <CENTER><B><FONT COLOR="#800080">$digsound[$c]</FONT></B></CENTER>
       </TD>
       <TD>
        <CENTER>$runtime[$m[$c]]</CENTER>
       </TD>
       <TD WIDTH="20%">
        <CENTER><FONT SIZE=+1>
          $starring[$m[$c]]</FONT></CENTER>
       </TD>
       <TD WIDTH="20%">
        <CENTER>$showtimes[$c]</CENTER>
       </TD>
       <TD WIDTH="20%">
        <CENTER><a href=$studiourl[$s[$m[$c]]]><IMG SRC="
http://domain.com/images/studios/$studio[$s[$m[$c]]].gif"
NOSAVE HEIGHT=60 WIDTH=115></CENTER></a>
</a>
       </TD>
      </TR>
EOF
   }
   $c++
}

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 ;
</TABLE>
<p><b><font size=4><center>
$message[$t]</b></p>
<P><BR><B><FONT COLOR="#3333FF"><FONT
SIZE=+1>Copyright 1998&nbsp;</FONT></FONT></B>
<BR><B><FONT COLOR="#3333FF"><FONT
SIZE=+1><A HREF="mailto:norman\@domain.com">MnM
Entertainment</A> &amp;</FONT></FONT></B>
<BR><B><FONT COLOR="#3333FF"><FONT
SIZE=+1><A HREF="mailto:webmaster\@domain.com">Jason
Shindler</A></FONT></FONT></B></CENTER>
</TD><TD COLSPAN="4">
</body>
</html>
EOF

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

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




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