10.6 Exercises
10.6 Exercises
1. (Easy: Documentation Reading)
Use perldoc to learn more about how to write modules that tie a variable to a module. Run
%perldoc perltie
to bring up the write-up on ties.
2. (Easy: Documentation Reading)
Read Perl documentation on the DBI.pm module. The documentation is fairly extensive and is about 50 pages long. In particular, learn about all the methods that database handles support. Also, learn about all the methods that statement handles support. In particular, learn the various ways in which results from execution of an SQL query can fetched from a statement handle.
3. (Easy: DBI Alternatives)
Rewrite the programs in Section 10.4 so that they use the various approaches to fetching selected rows. Compare the pros and cons of using fetchrow_arryref, fetchrow_array, fetchrow_hashref,
fetchall_arrayref and fetchall_hashref. Please note that not all these methods may be available in all versions of the DBI.pm module.
4. (Easy to Medium: Documentation Reading, Research)
In this Chapter, we have discussed how to use only MySQL databases. If you have access to any other databases, learn how to access them from Perl programs. Rewrite all the programs of this Chapter to work with the new databases. What changes to you have to make to the programs?
5. (Easy: DBM, Search, HTML Forms)
Suppose we need to record the following pieces of information for an individual: last name, first name, middle initial, social security number, street address, city, state and zip code. Find a representation for such data in terms of key-value pairs. Save the records in a DBM file for persistence. Populate your DBM file with at least 20 records.
Write subroutines that do the following.
(a) Search for individuals from a specific city and return individuals sorted by last name, first name, and middle initial.
(b) Write an HTML form and link a CGI program to the submit button to perform the search above. Return the results in a nicely structured HTML table.
One idea is to use the social security number as the key. Concatenate the rest of the fields using a pre-specified separator and store the resulting string as value.
6. (Easy: Database Search, HTML Forms)
Solve the previous problem using a database. Create the table with appropriate fields. Use any database to which you have access and is available on your machine or across the network.
7. (Medium: Recursive Structure, DBM)
Suppose we want to keep information about authors of books in a DBM file. For each author, we have the following pieces of information.
(a) Author ID: a positive integer,
(b) Author Name consisting of three parts: last name, first name, and middle initial,
(c) A list of books authored: For each book, there is a title, a publication year, and publisher information. Publisher information includes publisher’s name and the name of the city where the publisher is located.
Informally, the information about an author has the following information.
[authorID, [lastName, firstName],
[bookTitle1, publicationYear1, [publisherName1, publisherCity1]]
[bookTitle2, publicationYear2, [publisherName2, publisherCity2]]
[bookTitle3, publicationYear3, [publisherName3, publisherCity3]]
....
]
Thus, there can be one or more books an individual has authored. Can you represent the information as outlined above in terms of key-value pairs so that it can be stored in a DBM file? Think of serializing the data associated with an author.
Using a DBM file may not be the best approach in this case. However, you are encouraged to implement a program for this problem to gain valuable experience and insight.
8. (Medium: Database, Research)
Solve the previous problem using a database. It would be appropriate to define several related tables so that there is no redundancy is data storage. If you do not know how to do this, consult a tutorial book on databases. One such book is Fundamentals of Database Systems with E-Book (With CD-ROM) by Navathe and Elmasri [NE02].
9. (Medium: DBM, Encryption, Research)
Consider a record structure where we have a unique person ID, a last name and a first name, and his or her salary. We want to keep the salary information encrypted using DES or some similar private key encryption scheme. Thus, only those who can supply the proper secret key can read the information. Implement a program that prompts for a key and encodes the salary information using the encryption scheme before saving it to a hash. Store the hash’s contents in a DBM file.
Write another program that returns the salary for individuals with a certain last name, or a last name-first name combination. The program prompts for the key and provides the salary in unencrypted form only if the correct encryption/decryption key is provided.
Read Chapter 11 to review how encryption algorithms such as DES are used. Assume that the private key is distributed securely, say by hand, to parties who need to know it.
10. (Medium: Database, Encryption, Research)
Redo the previous problem by using a database. Use any database of choice.
11. (Medium to Hard: Database, CGI Programming, Search, Research)
Write a program to organize your music CDs. A record consists of the following information: Song title, Artist name, Album from which the song is taken, Genre, Time the song takes, Year it was published. Create an appropriate database table. Enter at least twenty rows of data in the table. Use any database of choice.
Create an HTML form that allows one to search for a song by one or more of the following criteria: artist name, song title, album, and genre. Note that only any one field needs to be specified. When the submit button is clicked, a CGI program returns the chosen songs in a nice HTML table.
Can you extend your program so that when a user clicks on a song’s title, the song is actually played on a audio player on the computer?
12. (Medium to Hard: Guest Book, CGI Programming, Search, Can be a long-term project)
Consider adding a guest book to a Web page that belongs to you. A guest book allows individuals visiting the Web page to leave comments. Include an HTML form on the page that allows an interested individual to enter the following information: a name, an email address, a text area where the individual can leave textual comments, geographic location of the individual leaving a comment, the date and time of the posting. Create a database table with appropriate fields. Write the CGI program so that a visitor can add his or her comments.
Sort the entries in the table by date and time. For each entry in the database table, enclose it within appropriate HTML tags to make it look nice on the Web page. Let the Web page clearly show the date and time of posting, the poster’s name, and his or her email. Create this page and provide a link to this page from your Web page. You can create this page by running a Perl program off-line.
Also, develop a simple form that allows a visitor to search for comments based on one or more of the following criteria: name of the poster, and the date of posting. Sort the entries by date. Write the CGI program to do so.
13. (Medium: tie, Research)
Write a Perl program that implements storing of an array in a text file. In particular, assume that the array contains the following elements that can be interpreted as fields of a record: VIN number of a vehicle, its make, model, color and approximate resale value. Write a tie module so that one can enter new vehicles into the text file, and that it can return a record given a VIN number. Assume that the VIN number is a 20 digit alphanumeric value. Keep the fields of a record in a single line of the text file. Separate the fields of the record using a pre-specified separator, say, one or more tabs, colons or commas.
14. (Medium: tie, Database, Research)
Redo the implementation of the tie in the previous problem so that the records are stored in a database table instead of a text file.
15. (Hard: Virtual Bookstore, Contains many components, Long-term project)
Redo the problem titled A Virtual Bookstore at the end of Chapter 8 on CGI programming. Store all your data in terms of database tables.
16. (Hard: Virtual Grocery Store, Contains many components, Long-term project)
Redo the problem titled A Virtual Grocery Store at the end of Chapter 8 on CGI programming. Store all your data in terms of database tables.
17. (Hard: Virtual Auction Site, Contains many components, Long-term project)
Redo the problem titled A Virtual Auction Site at the end of Chapter 8 on CGI programming. Store all your data in terms of database tables.
18. (Hard: Web-based Sports Site, Contains many components, Long-term project)
Redo the problem titled A Sports Site at the end of Chapter 8 on CGI programming. Store all your data in terms of database tables.
