From: Johan Meskens CS3 jmcs3 <> wrote:

: # here is what five.cgi consists of
: # ( it returns a random gif-image )
: 
: use warnings;
: use strict;
: 
: use CGI;
: 
: my $dirpath = '/Users/Shared/ChromaticSpaceAndWorld/INTRA';
: 
: opendir( DIR, $dirpath ) || die " espejo de vidrio $! ";
: my @files = grep ! /^[\._]/, readdir DIR;
: closedir DIR;
: 
: my $image = $files[ rand( $#files ) ];
: 
: my $img = CGI->new();
: 
: open( FILE, "$dirpath/$image" ) || die " espejo de vidrio $! ";
: 
: binmode FILE;
: binmode STDOUT;
: 
: print $img->header('image/gif');
: 
: print while <FILE>;
: 
: close FILE;
: 
: # so i remain with this second question
: # there is probably something fundamental i don't understand

    It looks like a browser caching thing. The first random image
is downloaded and the browser assumes all other references to that
image are the same image.

    You could do everything in the first program by avoiding the
actual delivery of the binary. This function will grab the image
file names when the program starts and load them into @files.
(@files is not directly available to the rest of the program.)

BEGIN {
    # path
    my $dir_path = '/Users/Shared/ChromaticSpaceAndWorld/INTRA';

    # grab the files
    opendir DIR, $dir_path or die "espejo de vidrio $!";
        my @files = grep ! /^[\._]/, readdir DIR;
    closedir DIR;

    # return a random file
    sub random_image_url {
        return '/INTRA/' . $files[ rand @files ];
    }
}

    random_image_url() will return a random image file name from
that array each time it is called. Here we use it to return six
random file names.

push @random_images,
        $q->img( { -src => random_image_url() } ) .
        $q->br()
            for 1 .. 6;


    And print it:

print
    $q->header(),
    $q->start_html( 'Six Random Images' ),
        @random_images,
    $q->end_html();


    Using it like this forces the browser to do the work. It
also gives the web surfer the option of turning off images or
using a proxy to rewrite them. Ultimately, I want to empower
the users of my site.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328








-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to