Computer Sciences Department logo

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

Due Friday, August 6th, at the start of class.


I have written a script. It may contain security and performance problems. Your job is to look at the script and fix any problems you find.


The script is included below. It is a variation of the word frequency script from Day 4 — it tallies word frequencies from words in a file and prints out ones with frequency greater than 1.


use Text::Wrap qw/wrap/;

$Text::Wrap::columns = 80;

my $filename = $ARGV[0];
my $filename2 = $filename;
$filename2 =~ s/\W+//g;

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]);

open(my $out, ">output-$");
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];

system("mv output-$filename output-$filename.bak");
system("mv output-$ output-$filename");

print "Done!\n";
exit 0;

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

./ homework-15-input.txt

There is no output. Instead, the script writes to an output file, “output-homework-15-input.txt”.

Analyze the script and rewrite it to fix any problems that you find. Turn in your rewritten script.


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(s). 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.