## Modules and setup use CGI; use HTML::Template; use strict; $CGI::POST_MAX=1024 * 1; # max 1k post $CGI::DISABLE_UPLOADS = 1; # No uploads # Your script name is already a property, # so this isn't generally necessary - # just call $ENV{SCRIPT_NAME} # instead of $ThisScript; my $ThisScript = $ENV{SCRIPT_NAME}; # better to create new CGI object this way with ->new() my $q = CGI->new(); my $template = HTML::Template->new( filename => "./template.html" ); # instead of making variables that we'll later use # only once, let's just do the math when defining # the template params: $template->param( PreviousPicture => $q->param('Picture') - 1 || NumberOfPictures(), PictureToView => $q->param('Picture') || 1, NextPicture => $q->param('Picture') % NumberOfPictures() + 1, ); print $q->header, $template->output(); { # we'll go ahead and cache the number for # future calls without re-reading the directory # we don't need a global variable for this, so # we'll limit the scope of this cached variable to # this block. my $cached_number_of_pics; sub NumberOfPictures { return $cached_number_of_pics if defined $cached_number_of_pics; opendir(DIR, "./") || die "Couldn't open directory: $@\n"; my @LFiles = grep { not /^(\.\.?|template\.html|pictureview\.cgi)$/ } readdir(DIR); closedir(DIR); # We don't really have a reason to continue # if there are no files, so die. die "No files -- I have no idea what to do\n" unless @LFiles; $cached_number_of_pics = @LFiles; # probably not necessary to force scalar # context, but... return scalar @LFiles; } }