jmac has asked for the wisdom of the Perl Monks concerning the following question:

I need to print out an html list of all images that are contained in a certain directory. Any suggestions on the best way of doing so?

Replies are listed 'Best First'.
Re: directory list
by Adam (Vicar) on Sep 20, 2000 at 03:23 UTC
    I know some people don't like glob, but I do.
    my @images = map {glob} ( '*.gif', '*.jpg', '*.jpeg' );
    Or you could glob the whole directory and then assume anythat was a file was an image... using -f as your check:
    my @files = grep { -f } glob '*'; # warning! some elements in @files might not be images.
Re: directory list
by merlyn (Sage) on Sep 20, 2000 at 05:07 UTC
    Hmm. Most people so far missed an important part of this list:
    I need to print out an html list of all images that are contained in a certain directory. Any suggestions on the best way of doing so?
    Which probably means you want to make a link for each, and you're doing it by running a CGI script of some kind. See my snippet "Show a clickable link list of images from a CGI script" for some code to do this.

    -- Randal L. Schwartz, Perl hacker

      oh, an html list...
      perl -e 'print "<html><body><pre>",`ls *jpg *png *gif`,"</pre></body>< +/html>"'
      Ooops.
      Well I'm not sure how much help jmac needed in the html department:
      use strict; use CGI; use CGI::Carp( 'fatalsToBrowser' ); use HTML::Entities; my $q = CGI->new(); my $dir = '\images'; # or some other default. chdir( $dir ) or die "Couldn't cd to '$dir', $!"; my @images = map {glob( "*.$_" )} qw( gif jpg jpeg tif xif bmp ); @images = map { encode_entities( $_ ) } @images; print $q->header(), $q->start_html(); print $q->h1( "My Image files" ); print "\n<UL>\n"; print "<LI>${_}\n" for @images; print "</UL>\n"; print $q->end_html();
      UPDATE:
      Rather then argue further with merlyn, I removed the user input part of my code. That's what I get for trying to add a little functionality... sigh.
        my $dir = $q->param('imageDir') ? $q->param('imageDir') : '\images'; # or some other default. die "Illegal directory name '$dir'" unless -d $dir; chdir( $dir ) or die "Couldn't cd to '$dir', $!";
        I'm not sure I'd ever install a CGI script that took an unrestricted directory name from a parameter! Danger, Will Robinson!

        -- Randal L. Schwartz, Perl hacker

Re: directory list
by arturo (Vicar) on Sep 20, 2000 at 03:25 UTC
    Use opendir and readdir (perldoc -f opendir and the same for readdir)
    opendir HTMLDIR, $directory_name or die "Can't open $directory_name: $ +!\n"; while (readdir (HTMLDIR)) { push @images, $_) if /\.(png|jpg|gif|tiff)/; }

    When you're done, @images will contain a list of the files with the extensions jpg, png, gif, or tiff (go wild here =)
    UPDATE : d'oh! the loop I wrote above doesn't set $_ as it loops through the directory. Try

    foreach ((readdir(HTMLDIR)) { # stuff }
    instead.

    And, while we're at it, you could do

    my @files = grep { /png|gif|jpg|tiff$/ } (readdir(DIR));

    For one-liner goodness (grep { condition } list grabs everything in list that meets condition)
    I daresay these may not be as efficient as Adam's solution above.

    Philosophy can be made out of anything -- or less

Re: directory list
by fundflow (Chaplain) on Sep 20, 2000 at 04:09 UTC
    Here are two short options:

    @images = glob('*.{tif,jpg,jpeg}'); or @images = <*.{tif,jpg,jpeg}>;

Re: directory list
by jmac (Initiate) on Sep 20, 2000 at 03:29 UTC
    thanks a bunch for the speedy replies!