Class #1

  • Introduction
  • Quick Review
  • New Stuff


    Basic Principles of CGI

    Steps:

    1. A user is sitting at his web browser and clicks on a link or a button that makes a request back to the website (like when you hit a 'SUBMIT' button to purchase something online).

    2. Information that the user has provided is passed to the web server over the Internet.

    3. The web server passes the user's request to a CGI program (a program written in Perl, for example).

    4. The CGI program can perform many actions - look in a database, send an email, create a user-customized page, etc. The final output of the CGI program is usually an HTML page that is sent back to the user's browser.


    Syntax Review

    Variable Examples:

    $myname = "Chris";
    print $myname;
    
    $pi = 3.14
    $twopie = 2 * $pi;
    print $twopie;
    
    @people = ("Jim", "Joe", "Bob", "Mary", "Sue", "Nicole");
    print $people[0];
    print $people[4];
    foreach $person (@people)
    	{
    	print "$person\n";
    	}
    
    
    $STUDENT{SSN} = "155-84-8674";
    $STUDENT{PHONE} = "201-555-1212";
    $STUDENT{EMAIL} = "student@school.edu";
    foreach $var (%STUDENT)
    	{
    	print "$var: $STUDENT{$var}\n";
    	}
    

    Using Perl Modules

    Imagine we have a Perl module that allows you to create your own very own monster. Your monster can do the following things: eat, sleep and run. The module comes with documentation that describes the following methods:

    NAME
    	Monster.pm - the simple monster interface class
    
    SYNOPSIS
    	#Allows you to create your very own monster.
    	
    	use Monster;
    	$monster = Monster->new;
    
    	#Make Your Monster perform some type of action
    	$monster->eat("steak");		#makes your monster eat steak
    	$monster->run("5"); 	#makes your monster run 5 miles
    	$monster->sleep("10");	#makes your monster sleep 10 Hours
    	
    	#Get some info about your monster
    	@what_did_he_eat = $monster->regurgitate; #shows what your monster ate
    	$miles = $monster->runsThisLong; #shows how long your monster ran
    	$hours = $monster->sleepsThisLong; #shows how long your monster slept
    
    Based on the description of the module above, we can create our own code using the monster module. Let's examine the following code:

    #!/usr/bin/perl
    
    # Monster object example
    # Christopher Uriarte 
    
    # Check for Perl modules in the present directory
    use lib `pwd`;
    
    # Tell Perl to use the Monster module
    use Monster;
    
    # Create the monster...could also use Monster->create
    my $monster = Monster->new;
    
    # Feed the monster
    print "Feeding my monster small children, apples and potato chips:\n";
    $monster->eats("small children");
    $monster->eats("apples");
    $monster->eats("potato chips");
    
    # See what the monster ate - return the contents of what he
    # at to an array called @he_ate. 
    @he_ate = $monster->regurgitate();
    
    # Here, we print out every element of the array @he_ate
    foreach $element (@he_ate)
            {
            print "My monster at $element\n";
            }
    print "\n";
    
    # Make the monster run 1 mile
    print "Making my monster run 1 mile:\n";
    $monster->runs(1);
    
    # Make the monster run another 5 miles
    print "Making my monster run 5 miles:\n";
    $monster->runs(5);        
    
    # See how much the monster ran.  Put the value in a scalar 
    # variable called $miles
    $miles = $monster->runsThisLong();
    print "My monster ran $miles miles\n";
    print "\n";
    
    # Make the monster sleep 8 hours
    print "Making my monster sleep 8 hours:\n";
    $monster->sleeps(8);
    
    # See how much he slept.  Put the value in a variable called
    # $hours
    $hours = $monster->sleepsThisLong();
    print "My monster slept $hours hours\n";
    
    (if you are interested, this program actually does interface with a module called Monster.pm)


    Flat File Databases

    A common type of flat file database format is called the CSV format (for Comma Seperated Values). CSV files follow this format:

    	VALUE A,VALUE B,VALUE C,VALUE D				(Record 1)
    	VALUE A,VALUE B,VALUE C,VALUE D				(Record 2)
    	VALUE A,VALUE B,VALUE C,VALUE D				(Record 3)
    
    This format puts each record on its own line and separates each field by a comma. You can export a Microsoft Excel spreadsheet or Microsoft Access database table into .CSV format. For example, this Excel file can be exported to a .CSV file in this format:

    	NAME,EMAIL,PHONE
    	Chris Uriarte,chrisjur@cju.com,555-1212
    	Joe Blow,joe@hotmail.com,666-8888
    	Jane Doe,jane@msn.com,888-8808
    

    Similarly, you can create your very own, "home-grown" flat file format. For example, you can separate all records with 2 tildes (~~):

    	NAME~~EMAIL~~PHONE
    	Chris Uriarte~~chrisjur@cju.com~~555-1212
    	Joe Blow~~joe@hotmail.com~~666-8888
    	Jane Doe~~jane@msn.com~~888-8808
    

    In these cases, the commas and double tildes are called the field separators or delimiters.


    Sprite Examples

    Let's work with the flat file database with a 2-tilde record delimeter, as described above:

    	NAME~~EMAIL~~PHONE
    	Chris Uriarte~~chrisjur@cju.com~~555-1212
    	Joe Blow~~joe@hotmail.com~~666-8888
    	Jane Doe~~jane@msn.com~~888-8808
    
    Let's save this information in a file called NameDatabase.db.

    Here's an example of how we use Sprite to find all records in our dataset and print each record out in a readable format:

    # Tell Perl to use the Sprite module
    use Sprite;
    
    # We must first create a new object that is liked to the Sprite module.  We'll call
    # this object $query.
    $query = new Sprite();
    
    # We tell Sprite that our records are separated by ~~ characters
    $query->set_delimiter("Read", "~~");
    # We actually do the query placing its results in the @data array
    @data = $query->sql("select * from NameDatabase.db");
    $query->close;
    
    # The first record Sprite returns is a 'staus' record, telling us if 
    # everything in the query went OK.
    $status = shift(@data);
    
    # Now, we cycle through each element the @data array to print out our 
    # query results. 
    foreach $record (@data)	{
    	($name, $email, $phone) = @$record;
    	print "NAME:  $name\n";
    	print "EMAIL: $email\n";
    	print "PHONE: $phone\n\n";
    	}