A style note to begin with - try indenting your code. It's survivable for a simple script like this, but you'll easily trip up on more complex ones. Any decent editor will support you with that. There's also Perl::Tidy.

Why are you looking up the time separately for each part of the script? You can just reuse the variables from the start.

I would also suggest you accumulate your data first, then output all the results in one fell swoop. Littling prints all over the place makes CGI scripts very hard to maintain.

Most of your repetitions are easily avoided if you just use some math.

Math:

my $newjobtime = $min % 10;
Logic:
if ($hour = 1 or $hour = 9 or $hour = 17 or $hour >= 24) {
Now we see the "hot" periods are spaced 8 hours apart, so we can again go by the remainder. By subtracting one hour first, we can account for the fact that they lie on 1/9/17 rather than 0/8/16. But that will sometimes give us "-1 hours left", so we add 8 hours first, which doesn't change the result of the modulo but does make sure we always have positive values. Then, whether it is a now a hot period or not is only a matter of checking whether there are any hours left till the next one.
my $snowagerleft = ($hour + 8 - 1) % 8; if($snowagerleft) { # "$snowagerleft until awakens" } else { # "steal your keep" }
The following math in your code makes no sense:
if ($hour < 2) { my $newday; $newday = 2 - $hour; # ... } elsif ($hour >= 2) { my $newday; $newday = 24 - $hour; # ... }
My guess is that it's attempting to compensate for a timezone offset of two hours. In that case you can just subtract from 24, then massage the result. Math again:
my $newday = 24 - $hour; $newday = ($newday + 2) % 24;

Also note how you are repeatedly using the same bit of HTML to display each section's results. You should really use a templating system, but at least rather than copypasting those bits all over the place, you should just pull them into a subroutine.

Finally, your HTML could be a bit cleaner. :) If you use CGI's HTML generation routines, it will do a lot of the job for you on its own.

Everything put together:

#!/usr/bin/perl use warnings; use strict; use CGI qw(:standard *table); sub format_section { my ($width, $title, $message) = @_; return Tr( td({width => $width, bgcolor => "gray" }, $title), td($message) ); } my ($sec, $min, $hour) = localtime(time); my $newdayleft = 24 - $hour; $newdayleft = ($newdayleft + 2) % 24; my $hourminsleft = 60 - $min; my $newjobleft = $min % 10; my $snowagerleft = ($hour + 8 - 1) % 8; my $snowagermsg = $snowagerleft ? ( "There are $snowagerleft hours(s) " . "and $hourminsleft minute(s) left until he awakens!" ) : ( font({ color => "blue" }, q(The snowager is sleeping!)) . q(&nbsp;) x 34 . a({ href => "http://www.neopets.com/winter/snowager2.phtml", target => "_blank" }, q(Steal your keep!)) ); print header . start_html . h2({align => "center"}, "Important Neopets Times") . br() . "The current server time is $hour:$min:$sec" . start_table . format_section(15, Jobs => ( "There are " . font({ color => "blue" }, $newjobleft) . " minute(s) until jobs are replenished." )) . format_section(15, Snowager => $snowagermsg) . format_section( 20, "New Day", "There are $newdayleft hour(s) " . "and $hourminsleft minute(s) until the new day." ) . end_table . end_html;

Makeshifts last the longest.


In reply to Re: Neopets Times (program repair shop) by Aristotle
in thread Neopets Times by sulfericacid

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.