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 3

By Jason Shindler

Getting Started

The HTML

Before writing anything in Perl - you'll have to have some way of accessing the Perl program from your existing HTML pages. There are two ways to do this.

  • You can use HTML forms. Ask the user to specify which theater and which day they want to search for. They choose from a list using HTML forms.
  • Just specify a URL. This is the simplest and requires less user interaction.

For example:

http://domain.com/cgi-bin/moviewizard.cgi? showdate=07011999&theater=1

Put a link similar to this one in your HTML. This will work in some simple cases, but the first choice is far more flexible.

On to the Perl

To start the project, we must start two header lines. The first tells the system where to find the Perl executable (this may vary on some systems). The second line tells Perl to import the libraries that will translate the HTML form output into Perl.

#!/usr/bin/perl
require "cgi-lib.pl";

Next we have to import in the HTML form output. The first line is the only necessary one. The rest of the lines just save time later. For instance - if you include the optional lines, you could write $printerver later in your script instead of writing $input{'printerver'}.

ReadParse(*input);
$datetosearch = $input{'showdate'};
$showthisdate = substr($input{'showdate'}, 0, 2);
$showthisdate1 = substr($input{'showdate'}, 2, 2);
$showthisdate2 = substr($input{'showdate'}, 4, 4);
$theater = $input{'theater'};
$movienum = $input{'movienum'};
$t = $theater

This is some more "getting started" stuff. This section of code imports the current system time into a series of variables.


($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$thismonth = $mon + 1;       # month in perl is 0-11, must add 1
$thismonth1 = (Jan,Feb,Mar,Apr,May,June,July,Aug,Sept,Oct,Nov,Dec)[$mon];
$year = $year + 1900;        # After year 2000, year comes in form 
                             # 100 = 2000, etc. So this works
if ($thismonth < 10)
   { $thismonth2 = join '', 0, $thismonth; }
else
   { $thismonth2 = $thismonth }

if ($mday < 10)
   { $thisday = join '', 0, $mday; }
else
   { $thisday = $mday; }

$time0 = (join '', $thismonth2, $thisday, $year);

On our HTML pages, the user has the choice of picking a specific day to view or one can choose "today." This part computes what the day is and puts it in the appropriate variables for computations. The $showthisdate = substr($time0, 0, 2) command takes the variable $time0, starts reading at character 0 (the first one) and reads for two characters and returns the information into the variable $showthisdate.

if ($input{'showtoday'} eq "yes")
   { $datetosearch = $time0;
   $showthisdate = substr($time0, 0, 2);
   $showthisdate1 = substr($time0, 2, 2);
   $showthisdate2 = substr($time0, 4, 4);
   }

Now, we must start bringing in the data from our text files, which -- in this case - contain information that is specific to each theater. We'll repeat a similar process for each of our flat-file databases. In our example, they include information about each movie, each showtime and each movie studio. Other applications of this technique might include a list of recipes (with ingredients, instructions, etc.), or a list of contacts (with names, addresses, etc). This piece of code cycles through each line in the file and extracts this information into a series of variables. Everytime it hits a semicolon in the file, it puts the next information from the file in the next variable. Every time it hits a line break, it increments $t and starts the process over again. It ends the loop when there are no more lines.

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

while ($line = <THEATER>)
{
   ($theaterpic[$t], $adultprice[$t], $childprice[$t],
    $seniorprice[$t], $matineeprice[$t], $message[$t],
    $phone[$t])	= split /;/, $line;
   $t++
}

The process is repeated for the movie text file. Note the variable names are different. They indicate the different information that comes from this file.

open(MOVIE, "movie.data") 
   or die "Domain.com could not open the Movie Information file.\n
           Please email webmaster at domain.com with the following 
           information:\n$!\n";
$d=1;
while ($line = <MOVIE>)
{
   ($name[$d], $website[$d], $starring[$d], $runtime[$d],
    $s[$d], $pic[$d], $rated[$d], $plot[$d]) = split /;/, $line;
   $d++
}


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