Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here is my go at it. I think a bunch of your logic at the bottom is difficult to read at first glance, so I tried to simplify it based upon the first numbered picture being 1 (and thus not 0).

The logic for previous, next, and this picture all works, unless there are no files (and thus my die statement when reading the number of pictures, if it is 0). It might be even better to break that math out into some subroutines, especially if you will be doing it in more than one place.

This is untested, as I didn't take the time to write the accessory templates, but this should be a good starting place. (comments added where I deemed necessary)

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

In reply to Re: How can it be done better? by perlguy
in thread How can it be done better? by SilverB1rd

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-03-28 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found