Computer Sciences Department logo

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

Due Friday, August 5, at the start of class.


I have written a script. It might contain security, performance, or correctness issues. Oh, let’s be realistic, it’s full of egregious problems! Your job is to look at the script and fix every problem that you find. Consider this your final exam, there is a little bit of everything in here…


The script is included below. It is a variation of the word frequency script from Day 4 — it tallies the frequencies of words found in a file and writes to a new file the ones with frequency greater than 5.


use POSIX qw/strftime/;
use Text::Wrap qw/wrap/;

$Text::Wrap::columns = 80;

my $filename = $ARGV[0];
my $safename = $filename;
$safename =~ s/^[<> ]+//g;    # Make filename safe

my @words;
open(my $fh, $filename);
while (my $line = <$fh>) {
    $line = lc($line);
    my $found = 0;
    foreach my $word_ref (@words) {
        if ($word_ref->[0] eq $line) {
            $found = 1;
    if (not $found) {
        push(@words, [$line, 1]);

my $outname = strftime('output-%Y-%m-%d-', localtime) . $filename;

open(my $out, ">$");
foreach my $word_ref (sort {$a->[0] cmp $b->[0]} @words) {
    my $freq = $word_ref->[1];
    next if $freq < 5;
    printf $out "%4d %s\n", $freq, $word_ref->[0];

# Safe file write
system("mv $outname $outname.bak");
system("mv $ $outname");

print "Done!\n";
exit 0;

Download the input file here. Run the script like this:

perl homework-15-input.txt

There is no output. Instead, the script writes to an output file (see the script for details).

Analyze the script and rewrite it to fix all of the problems that you find. Make it as good as you know how. Turn in your rewritten script.

Optional Extra

Prove that you made the script better. Different fixes might require different kinds of evidence.


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 script. 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.