Well to make it easier on me, I turned this into a command line function. Should be easy to convert back.
The ideas were solid, but with some improvements you can make this more solid and much easier to debug problems like this.
#!/usr/bin/perl -w
#use CGI; # you should be using CGI.pm
#use strict 'nuff said
use strict;
#You should factor out the directory name since you are using it in mu
+ltiple places;
my $HOMEDIR = '.';
#you should put the name of the file and a newline for errors (much ea
+sier to understand)
#you should avoid '||', use 'or' instead. 'or' will be what want most
+of the time.
# FYI '||' binds tighter than 'or;
opendir(HOMEDIR, "$HOMEDIR") or die ("Unable to open directory $HOMEDI
+R\n");
my @files = grep !/^\./, readdir(HOMEDIR);
closedir(HOMEDIR);
#You should learn to use $_, saves loads of typing
foreach (reverse sort @files) {
#check to see if it is actually a file (you don't want directories
+)
next if (!(-f "$HOMEDIR/$_"));
print "--->$_<---\n";
#same as above
open(STUFF, "./$_") or die ("Unable to open file $_\n");
while(<STUFF>) {
1 .. 5 ? print: last;
}
close (STUFF);
print "\n";
}
HTH
grep
grep> cd pub
grep> more beer