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

Hi guys,

This is my second atempt at a perl program, and I'm stuck. This code (below) work perfectly fine on a Win98 Personal Web Server, Perl 5. It doesn't display the images on my Mac OS X with apache.? Nor does it at my ISP, they are running Red Hat.

Can anyone help?

opendir BUFFER, "images/" or "Serious Headache! $!"; my @templist = readdir BUFFER; closedir(BUFFER); #Print the table of pictures.... jpg's and then gif's................. +............ my @results; my $count = 0; print "<table border='1' cellspacing='10' cellpadding='4'>\n<tr>\n"; foreach(@templist) { if($_ eq "." || $_ eq "..") { #Do Nothing. } else { if(substr($_, length($_)-4, length($_)) eq ".jpg") { print "<td align='center'><img src=\"images/$_\" border=\" +0\" alt=\"Loading...jpg\" width=\"40%\" height=\"40%\"><br />"; print substr($_, 0, length($_)-4) . "</td>\n"; $count++; } else { if(substr($_, length($_)-4, length($_)) eq ".gif") { print "<td align='center'><img src='images/$_' alt='Lo +ading...gif' width=\"40%\" height=\"40%\"><br />\n"; print substr($_, 0, length($_)-4) . "</td>\n"; $count++; } } # Write the end of line... if($count == 4) { print "</tr><tr>\n"; $count=0; } } } print "</tr></table>\n";
Thanks people!

Replies are listed 'Best First'.
Re: Image Gallery
by Dog and Pony (Priest) on May 11, 2002 at 18:29 UTC
    First off, have a look at CGI.pm. Then I can think of several possible reasons for this, although I have never run PWS.
    • You don't have a shebang line (something like #!/usr/bin/perl) that points to the perl binary. Apache and most other web servers need this to know what to execute with (even on windows). I can only assume that PWS ignore this, yes?
    • You don't print the "Content-type: text/html\n\n" header which needs to be the first thing out of the script. Again, see CGI.pm for more info.

    My best bet is that you are relying too much on special features in PWS. Hope this helps. :)

    Update: Since it is most likely that both Mac OS X and the Red Hat is using Apache, I suggest you take a look here.


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
      This is only a small snippet of the code the shebang line is in there. The Directoy is being opened by the CGI code so the file names that I am using are the exact same as the filenames. I will look into apache help on these issue, and thanks fory your help so far people.
      This is going to be a real stupid question, but If I don't ask I will never know. What are .pm files exspecially CGI.pm? thanks in advance.
        .pm files are perl modules - essentially resuable code that you can include and use. Have a look at perlmod and perlmodlib, then head over to http://search.cpan.org and see what an insane wealth of already invented wheels you have at your disposal. Tried and true solutions to common (and uncommon) problems people often bump into.

        CGI.pm is one of those. It is what basically *everyone* uses to do dynamic HTML in perl, and it is very thouroughly tested, plus it has tons of convenience methods for you to use. You will not regret it if you start to use that instead of rolling your own.

        You should already have CGI.pm and lots of other really useful modules included with your perl - try to type perl -V on the command line, and look at the directories that are listed under the entry @INC. Look in those paths, and there they should be. For documentation on them, use perldoc which should be included too, or go to http://perldoc.com.

        Hope that helps. :)


        You have moved into a dark place.
        It is pitch black. You are likely to be eaten by a grue.
Re: Image Gallery
by Zaxo (Archbishop) on May 11, 2002 at 21:44 UTC

    Perhaps your script is not finding the relative path 'images/'. To find the images from wherever the script runs, use an absolute path, or employ $ENV{DOCUMENT_ROOT}.

    You may find glob(*.{jpg,gif}) convenient, and CGI.pm has some very handy tricks for distributing tags over a list. Something like:

    print $cgi->Tr( $cgi->td( {-align=>'center'}, [splice @images,0,4] ) ), $/ while @images;

    CGI.pm has the special behavior of taking an array reference argument as a sign to map. $foo = CGI::footag(\@ary) is equivalent to $foo = join ' ', map {CGI::footag($_)} @ary;

    After Compline,
    Zaxo

Re: Image Gallery
by JayBonci (Curate) on May 11, 2002 at 21:49 UTC
    This may sound silly but another thing to look at is Apache is case sensitive, but PWS is not. Migrating from PWS to a normal web server means if you have things like photo.JPG (the way photoshop likes to name things) or Photo.jpg, a hit on photo.jpg will suffice on the former, but not the latter.

    What is readdir coming back with? Does the NIX / Win32 output match? By "doesn't display the images" do you mean that they show up as broken?

        --jb
        Your readdir is failing. The first line needs to do something if it's going to be effective. You have it has:
        opendir BUFFER, "images/" or "Serious Headache! $!";
        But you probably want to do:
        opendir BUFFER, "images/" or print "Serious Headache! $!";
        You can also make it a fatal error by using die "Serious Headache! $!" and use CGI::Carp(fatalsToBrowser).. Hope that helps. Also, you know what they say, while in Rome, make sure you have read and executable permissions on the Romans.

            --jb