mandog has asked for the wisdom of the Perl Monks concerning the following question:
The only moderately interesting thing is that the original get_files() used File::Find and was about 25 percent wallclock seconds slower.
#!/usr/local/bin/perl require 5.6.0; use warnings; use strict; use File::Spec::Functions; my $gthumb_dir=catdir('..','go','pics','thumbs'); my $gHTMLPath='/go/pics/thumbs/'; ################################## # # # no user servicable parts below # # # ################################## # FILE: listthumbs.pl REVISION DATE: 09-12-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 $myDir=shift; my @result_files; my @files; opendir(dirHandle,$myDir) || die "$! Couldn't open $myDir\n"; @files=readdir(dirHandle); foreach (@files){ if ($_=~m/\.((jpg)|(jpeg)|(gif)|(png))$/im){ push (@result_files,$_) ; } } closedir(dirHandle); return @result_files; } sub main(){ my @pics=get_files($gthumb_dir); @pics=map(catfile($gthumb_dir,$_),@pics); @pics=sort size_cmp @pics; for (my $i=0;$i<@pics;$i++){ $pics[$i]=~m/(\w*\.\w*$)/; $pics[$i]=$1; } print "content-type: text/html\n\n"; print "<!-- start code by $0 -->\n" ; my $cols=3; my $count=0; print '<TABLE>'; print '<TR>'; foreach (@pics){ $count++; --$cols; print "<TD width=200 align=left>\n"; print '<IMG SRC='. $gHTMLPath . $_ .'><BR>'."\n"; print ; print "</TD>\n"; if ($cols==0){ $cols=3; print "</TR>\n"; print '<TR>'; } } # end loop if ($count % 3!=0){print '</TR>';} print '</TABLE>'; print "there are $count thumbs<BR>\n"; print "<!-- end code by $0 -->\n" ; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: request criticism web page with images in dir (File::Find File::Spec)
by broquaint (Abbot) on Sep 13, 2001 at 14:10 UTC | |
by grinder (Bishop) on Sep 13, 2001 at 15:57 UTC | |
by mandog (Curate) on Sep 13, 2001 at 21:03 UTC | |
by broquaint (Abbot) on Sep 14, 2001 at 15:23 UTC | |
by mandog (Curate) on Sep 14, 2001 at 18:27 UTC |