In-Class Assignment 12
For this final in-class assignment, you have your choice of two differennt assignments. Choose the one that either interests you the most, or that you think will be easiest for you to complete.
Option A
This assignment will introduce you to signal handling in Perl.
Download or copy ~lallip/public/ic12.pl.
This program simply has an infinite loop to print out the current
date/time every five seconds. It can be terminated by sending any
signal to it. Add signal handling so that the INT, QUIT, and ABRT
signals will only terminate the program if the same signal is sent
within a five second gap.
Every time the program receives one of these signals, print out which signal was received. Then check the last time that signal was received. If it was within five seconds, exit the program with a message stating this.
Hint: The built-in function time() returns the number
of seconds elapsed since "the epoch" (January 1, 1970).
Option B
Download or copy the files
~lallip/public_html/tiehash.pl
and
~lallip/public_html/SortedHash.pm.
SortedHash.pm begins the definition of a module whose objects will be tied to hash variables. This object will pretend to be a normal hash, whose keys are always kept in a sorted order. The object has three data members: a reference to the "actual" hash, a reference to an array of keys of this hash, and an integer that keeps track of where in the sorted list of keys is currently being read
Most of the functions necessary for tie'ing have already been written for you. You
must fill in the subroutine bodys for STORE, DELETE, FIRSTKEY,
and NEXTKEY.
Approach
Whenever a value is stored into or deleted from the hash, update the array of keys to be equal to a sorted list of the now-modified keys of the hash.
Whenever the keys are iterated over, first reset the index value to 0, and then return the key corresponding to the current index number.
Warning
After the last key has been read, make certain that theNEXTKEY function returns
undef. This is how Perl knows the last key has been read. Otherwise, it will be in
an infinite loop, trying to read more keys.
Submission
To submit either version of this program, run~lallip/public/ic_submit.pl
on rcs-sun4.rpi.edu. Remember to choose only ONE
option. I will look at only the last submission you make.
Your submission is due at 6pm today, December 7,
2005.
