Thanks for your suggestions.
I switched to file globbing. It seems simpler and is built into perls greater than 5.6.0
I eliminated one of the map lines as the glob now returns the full path. I think this sped things up a bit
I switched to mod ing by three as you suggested. % is probably 100 times more expensive than addition and eliminated but 1/100000 of a second isn't much more than a 1/1000 and the modulo is easier to read.
I left the error checking in. --A few times it was handy to check to see that stat actually succeeded
I left the sort in, I do want the most recent images first. I also suspect that the $a $b variables supplied by sort are quicker than $_[0] and $_1
I left the printed comments and the print "content-type: text/html\n\n"; in as the former help me figure out problems when looking at HTML source and the later <large>:-></large> is needed for the HTML to display.
#!/usr/local/bin/perl
require 5.6.0;
use warnings;
use strict;
use File::Spec::Functions;
use File::Basename;
my $gHTMLPath='/go/pics/thumbs/';
my $gthumb_dir=catdir('..','go','pics','thumbs');
##################################
# #
# no user servicable parts below #
# #
##################################
if ($gthumb_dir=~m|/|){
$gthumb_dir.='/';
}elsif ($gthumb_dir=~m|\\|){
$gthumb_dir.='\\';
}elsif ($gthumb_dir=~m|:|){
$gthumb_dir.=':'; # we're on a Mac ??? not tested
}else{
die "on unknown OS or couldn't get path at start \n"
}
# FILE: listthumbs.pl REVISION DATE: 09-13-2001
#
# CGI to list display jpeg, gif & png files in a directory
#####################
# Declarations #
#####################
sub get_files($); # get a list of jpeg files;
sub size_cmp(); # used in sort function compares two values
sub main(); #
######################
# Run our program #
######################
main();
######################
# Definitions #
######################
sub size_cmp(){
my @a_stat=stat($a) or die "couldn't stat\n $a \n$!\n";
my @b_stat=stat($b) or die "couldn't stat \n $b\n$!\n";
#7 is size so
# sorting by size puts the duplicates next to each other
#9 is modification time
# sort reverse order
return $b_stat[9] <=> $a_stat[9];
}
sub get_files($) {
my @result=glob("$_[0]*.{jpg,jpeg,png}");
if (0==@result) {
die "couldn't glob in get_Files()";
}
return @result;
}
sub main(){
my @pics=get_files($gthumb_dir);
@pics=sort size_cmp @pics;
for (my $i=0;$i<@pics;$i++){
$pics[$i]=basename($pics[$i]);
}
print "content-type: text/html\n\n";
print "<!-- start code by $0 -->\n" ;
my $count=0;
print '<TABLE>';
print '<TR>';
foreach (@pics){
$count++;
print "<TD width=200 align=left>\n";
print '<IMG SRC='. $gHTMLPath . $_ .'><BR>'."\n";
print ;
print "</TD>\n";
if (0==$count% 3){
print "</TR>\n";
print '<TR>';
}
} # end loop
print "\n</tr>" if 0 != (3 % $count);
print '</TABLE>';
print "there are $count thumbs<BR>\n";
print "<!-- end code by $0 -->\n" ;
--mandog |