Programming in Perl
Homework 1

Due Tuesday, 9/17/2002, 11:59:59pm

FAQ now available. Check it frequently.

Part A

Write a perlscript that will ask the user for the name of a file to open in the current working directory. If that file does not exist, ask the user if he/she wishes to enter the name of a directory that does contain the file. If the user indicates yes, allow the user to enter that directory name. Then attempt to open that file.

If the 2nd attempt fails, or if the user indicates he/she does not wish to enter a directory name, you may exit the program, giving an appropriate error message.

When the file has been opened, read from the file and display the following information:

Once this information has been displayed, open a new file (in whatever directory the orginal file was located). This file should be named the same as the old file, but with all capital letters. (ie, if the original file was "File1.TXT", the new file is "FILE1.TXT"). You should then print to the new file the contents of the original file, but in all lowercase letters. Don't forget to close the file.

Part B

Write a perlscript that takes one or more command line arguments. These arguments are files to be opened for reading in the current working directory. You should open each file in succession and read from it. Keep track of the number of times each "word" appears in all files combined. (For the purposes of this assignment, a "word" is any string delineated by spaces). Once all files have been read, print to the screen each word and the total number of times it appeared. Note that if any of the command line arguments cannot be read, a suitable error message should be displayed, but the program should not end.

Combining the parts

Your homework should be all in one file. Both parts of the assignments are within one single perlscript. Your program decides which part to execute based on whether or not there are any command line arguments. If 1 or more arguments are provided, part B exectues. If not, part A executes.

Grading Criteria

Part A
prompt for filename:5
prompt for directory if file does not exist:5
open file:5
exit on 2nd attempt, or user doesn't wish to enter directory:5
print number of lines:5
print first and last line of file:10
create and print to new file:10
Part B
open each file:5
Read and keep track of word count:15
print number of times each word appears:10
Combined
Parts combined correctly:5
Compilation:5
Code Style:5
Output Style:5
Error Checking & Reporting:5
Compilation means that your code executes with no errors and no warnings, and that it makes a valid attempt at solving the assignment. A blank file with the shebang will not earn 5 points for "compiling"
Code Style refers to the way your code looks. Proper indentation, meaningful variable names, and plentiful and/or witty comments are rewarded. Code that has no comments, code that comments every single statement, code with variables named $a, $b, $c, $d, $e, etc, and code that is painful to look at will all be penalized.
Output Style refers to the readability of the output of your script. Note that your homework is being run and graded by a human being, not a machine. Every piece of output should be explicitly labeled, and formatted so a human being does not go cross eyed trying to read it.
Error checking means that your program gracefully handles user stupidity. Any forseeable error by the user should be accounted for. (for example, what if none of the command line arguments are valid filenames?)

Above & Beyond

A maximum of 5 points will be awarded for significant contributions to the homework assignment. This means the implementation of a requirement not listed above. It does not mean making your code more efficient, cuter, more portable, or fancier. Basically, think to yourself "If I was giving this assignment to the class, what *else* might I require the program to do?"

Submission instructions

There are two methods of submitting your homeworks. These methods will be used for all homeworks in the course.
Method 1 - RCS program
Within Paul's public directory, there is a file called submit.pl. Run this file from your own RCS account (preferably from within the directory containing your homework). That is, log into RCS, and issue the following command:
~lallip/public/submit.pl
This program will ask you for information and for the name of your homework file. Fill out all information, and be sure the confirmations reflect reality.
Method 2 - CGI program
Go to http://cgi2.cs.rpi.edu/~lallip/cgi-bin/perl/fall02/submit.cgi. Fill in all the fields appropriately, and click Submit. Read the confirmation screen carefully to check for any discrepancies.
For either method, you may submit as many times as you deem necessary. Only the final submission will be looked at and graded.

Sample I/O

Part A
Part B

Your program must run properly on the machine rcs-sun1.rpi.edu

Homework 1 is due at 11:59:59pm EDT on 9/17/2002