Practice with Hashes and Databases in Perl

Prep readings:

In this assignment, we wish to:
- learn how to use the hash data type in Perl
- learn how to create and use simple databases in Perl
What You Need To Do
- Begin reading Chapter 8 of your text.
As you encounter example code, execute the Perl programs provided.
- Now, carefully study the code in the hashing.pl file provided above. If something in
the file doesn't make sense, use your book or on-line Perl Websites to add comments
to the code so the meaning is clear to you.
- Next, execute this program. What type of output do you get? What is different about
the various methods for displaying the information in the hash variable?
What output files does this program create? Can you read these files?
Why not just use the hash data type in the program, without any DBM files?
- Now, add a subroutine to this file called query_db. This subroutine should
ask the user for a key that he/she wishes to search for in the database. The subroutine
should then display the information from the database associated with that key.
When developing your query_db subroutine,
make sure the code handles the case where
there is no matching key.
If the key is not found in the database, the value returned will be undefined
(stored as a value which is evaluated as false in Perl). Check for this using an
if statement and display a message indicating that the key was not found.
You should also set the returned value to some default text that can
displayed from outside the function.
Sketch out your thoughts on how to write this subroutine on paper first,
then type your thoughts into the program, FIVE lines of code at a time. Be prepared
for the Edit-Run-Revise (and Save) cycle each time.
- Now, using this file as a guide, create your own example Perl program that uses
a hash variable and a DBM file. Your example must relate to your term project
in some way, so give this some thought before you get started on the changes.
You Perl program must have subroutines to create, display, and query the database.
- When you have completed the above steps and tested your program thoroughly, be sure
to make these additional changes:
- Break your program into two files, a driver program file and a subroutine module file.
Test your program to make sure it still works after you break it up.
- Make sure you have at least one subroutine which uses pass by reference. This subroutine
does not necessarily need to relate to your new functionality on creating and displaying
hashes and database. Again,
give this some thought before you get started on creating a new subroutine that
uses pass by reference. For
example, you may be able to modify some of your nucleotide counting subroutines
from other labs for
this part. You could then call that subroutine, passing some of the data (e.g.
a particular value) derived
from the database.
- Post your completed files on your course Web page. Choose whatever names you prefer.
Feel free to see how other students in the class are doing on the assignments.
These pages are optimized for viewing
under Netscape.
© Copyright 2003.
Melanie A. Sutton, Ph.D.
(msutton@uwf.edu)
All rights reserved.