Computer Sciences Department logo

CS 368-1 (2011 Summer) — Day 9 Homework

Due Tuesday, July 26, at the start of class.


Read a Perl script as input, and do two things:


The script takes the name of a Perl script as input: You can ask the user for the filename or simply hard-code it into your script. It will also save a new version of the Perl script: Either hard-code the new filename into your script or (more advanced) modify the given input filename to include “-new” right before the “.pl” extension.

Code Statistics

We would like to report on the code itself. Implement at least two of the following reports (arranged very roughly in order of easiest to hardest):

Fixing the Code Format

Everyone makes mistakes formatting their code now and then. The script will fix certain formatting mistakes and save the new, rewritten version of the code to a file. Implement at least one of the following fixes (again, arranged very roughly in order from easiest to hardest):

Note: In practice, I would not really do this to Perl code, because there are always reasonable exceptions to code formatting rules. But it’s good practice…

Example Output

A sample of the report output is below (taken from my solution to Homework #5).

Lines of Code:     51
Subroutine Names:  convert, get_number, get_unit
Scalar Names:      character, from_unit, i, length, meters, ok, result, to_from, to_unit, unit, units, value


Repeated Matches

You may want to match every instance of a pattern in a single string. How do you loop through them? It is much like doing repeated substitutions, use the g modifier:

while ($string =~ /...(...).../g) {
    # do something with each match within $string


Do the work yourself, consulting reasonable reference materials as needed; any reference material that gives you a complete or nearly complete solution to this problem or a similar one is not OK to use. Asking the instructors for help is OK, asking other students for help is not.

Hand In

A printout of your code on a single sheet of paper (if possible). Be sure to put your own name in the initial comment block of the code. Identifying your work is important, or you may not receive appropriate credit.