use strict;
use warnings;
use Cwd;
use URI::file;
use HTML::Entities;
 
my $toc_file = "toc.html";
if ($#ARGV > 0) {die "Incorrect usage. Use \"$0 dir_name\" or just:
\"$0\"\n"}
my $dir_name = ($#ARGV == 0) ? $ARGV[0] : cwd();
opendir(DIR, $dir_name) or die "Can't opendir $dir_name: $!\n";
my @list = sort {lc($a) cmp lc($b)} readdir(DIR);
chdir $dir_name or die "Couldn't chdir to $dir_name for some reason...\n";
open(OUT,">$toc_file") or die "Couldn't open $toc_file for writing: $!\n";
print OUT "<html><head><title>Table of Contents for ";
print OUT encode_entities($dir_name),"</title></head><body>\n";
print OUT "<h1>Table of Contents for ";
print OUT encode_entities($dir_name),"</h1>\n";
for (@list){
   next if (($_ eq ".") or ($_ eq ".."));
   my $u = encode_entities(URI::file->new($_));
   print OUT "<div><a href=\"$u\">$u</a></div>\n";
}
print OUT "</body></html>";

__END__

Note that my solution will not work for ALL cases- just the more common.
Plus, the HTML file created is very simplistic.
A better ('better' as in more complete/robust) solution would perhaps be to
determine the proper encoding (either based on the filenames or through a
command-line switch) and create a (valid) XHTML file, properly formatted for
that encoding. Or perhaps use UTF-8 regardless.
Another point is that I haven't got a way to test this on different file
systems, so I'm not sure it will work everywhere- although I've done my best
to try to make the solution robust in that sense. But I'm pretty sure that
the script will not currently work across file systems.
Finally, the toc.html file gets created in the directory which is examined-
this is on purpose. Since the quiz doesn't say anything about this, I feel
free to do what I think is right, and I think it makes more sense to place
the TOC file in the directory it refers to.

Offer Kaye

