I'm be grateful for any criticism. This is pretty trivial code, so I'm not expecting much...

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" ; }

In reply to request criticism web page with images in dir (File::Find File::Spec) by mandog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.