Programming in Perl

Homework 1

Due: Tuesday, February 4, 11:59:59pm ET

Sample Input and Output now available for part B.

Frequently Asked Questions now available. Check it frequently

Part A

Prompt the user for a directory name. If a directory with that name does not exist, print a suitable error message and exit the program.

Determine the number of files which currently exist in this directory. Print a message to the screen saying the number of files that exist, followed by the names of those files. Prompt the user for which file to open by asking for a number, not a file name (If the user enters 1, you open the first file listed, if the user enters 3, open the 3rd file listed, etc).

Determine the number of lines in the file and print out the 'middle' line (if there are 11 lines total, you would print the 6th). If there are an even number of lines, print both 'middles' (if there are 12 lines total, you would print the 6th and 7th).

Re-open the same file for appending. Print to the file the original first and last lines, but in all capital letters. Close the file.


Part B

Examine the command line parameters. The first paremeter is a file to open in the current working directory. If the file does not exist, exit the program with a suitable error message. The second parameter is a command. It will be either count, sort, or both.

If the command is count, display a list of the number of times each word appeared in the file. If the command is sort, print a list of all the words, sorted in ASCIIbetical order. If the command is both, first display the count of all words, then display the words sorted in ASCIIbetical order.

For the purposes of this assignment, a 'word' is a sequence of characters seperated by a space.


Combining the parts

Your homework is to be submitted in one file, and one file only. You are to combine the two parts as follows: If no command line arguments are given, your script executes Part A. If any non-zero number of command line arguments are given, your script executes Part B. If command line arguments are given, but are in some way invalid (wrong number of arguments, wrong 'type', etc) your program should print an appropriate error message and exit.

Grading Criteria

Part A
Prompt for and read directory name:5
Exit if directory does not exist:5
Print number of files:5
Print list of files:5
Open file specified by number user entered:5
Print middle line or lines:5
Print first and last lines, in caps, at end of file:5
Part B
Exit program if file does not exist:5
Display count of all words:15
Display all words sorted:10
Display count and sort if command is 'both':10
Caombined
Correctly combine parts5
Compilation:5
Code Style:5
Output Style:5
Error checking and reporting:5
Above & Beyond5

Grading Notes

Compilation refers to a valid attempt at solving the homework. A blank file with the shebang will not receive 5 points because it "compiles". Points will be subtracted here for any warnings or errors produced by your script (so don't forget to use -w).

Code Style refers to how your actual code looks. It should follow a logical indentation pattern, and have relevant comments. Note that obvious comments such as:
$i++; #Increment $i
are needless and may lose points.

Output Style refers to how your script's output looks. It should be human readable, and all values printed should be identified. ie, "Number of lines in the file: 20" rather than simply "20"

Error Checking and Reporting refers to your script being dummy-proof. You should account for anything the user can do that's wrong. Your script must never crash or hang. Specific examples in this assignment include (but are not limited to): verifying a filename is valid, the user enters a number in the correct range, the user enters the correct number and type of command line arguments.
Exception for this homework only: The method for determining if a string represents a valid number involves regular expressions, which we have not yet covered. Therefore, you may presume in part A that the user enters an actual integer number (but not that he/she enters a "valid" number).

Above & Beyond refers to an addition you make to your program that was not requested in the homework description. Please note that this must be an addition that extends the functionality or usability of your program. It must therefore be visible to the end-user without looking at your code. Examples of NOT valid A&B attempts are: Plentiful comments, making code more efficient, using a particular data structure or algorithm, formatting the output, or doing something that has nothing at all to do with the rest of the program (like programming a solitare game if I push 'S'). When thinking of an Above & Beyond submission, ask yourself what other features of this program I could have told you to implement, but did not.


Submission Instructions

There are two different ways of submitting your homework. The first is via a CGI script located at http://cgi2.cs.rpi.edu/~lallip/cgi-bin/perl/spring03/submit.cgi Enter your RCS Id, choose the homework number, and select your file.
The other method is via a program in Paul's RCS account. It is located at ~lallip/public/submit.pl. You will be prompted to enter the same information as above.

Whichever method you choose to submit your program, note that you are permitted to submit one file per submission only. In other words, no zipped files containing multiple source code or read me files. One perlscript per submission.

You may submit each homework infinite times. Only the last submission for each person will be looked at or graded. However, if you do need to resubmit, please make sure you re-submit using the same method you used for the original submission.


This program is due Tuesday, February 4th at 11:59:59pm. It must work correctly on the RCS machine rcs-sun1.rpi.edu