Date Problem

G

Graham Stow

The following script (or something much like it) runs continuously on a web
server and does something when the date changes (i.e. at the bewitching
hour). Problem is, it does it 2 or 3 times, when I want it to do it only
once. Any ideas anyone?

$start_date = "2008-09-20"; # this 'date' (string) will never change
and could be anything
&get_todays_date;
$date = $todays_date;
while ($date ne $start_date) { # the 2 'dates' will never equate, so
this script runs forever
&get_todays_date;
if ($date ne $todays_date) {
$date= $todays_date;
do something.....
}
}

sub get_todays_date {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year=$year+1900;
$mon=$mon+1;
if (length($mon) eq 1) {
$mon = "0"."$mon";
}
if (length($mday) eq 1) {
$mday = "0"."$mday";
}
$todays_date = "$year"."-"."$mon"."-"."$mday";
}
 
T

Tad J McClellan

Graham Stow said:
The following script (or something much like it) runs continuously on a web
server


That is likely to waste a boatload of cycles.

It would probably be better to run it once a minute or so using
whatever job scheduling your OS provides, such as "cron".

and does something when the date changes (i.e. at the bewitching
hour). Problem is, it does it 2 or 3 times, when I want it to do it only
once. Any ideas anyone?


I do not see how it is happening more than once...

$start_date = "2008-09-20"; # this 'date' (string) will never change
and could be anything
&get_todays_date;


You should not use an ampersand on subroutine calls unless you know
what it does, and what it does is what you want to do (it seldom is).

get_todays_date();

$date = $todays_date;


Communication via global variables is a horrible idea. You should instead
communicate via subroutine arguments and return values:

$todays_date = get_todays_date();

while ($date ne $start_date) { # the 2 'dates' will never equate, so
this script runs forever


Why take that chance? If you want a truly infinite loop (which I don't
think you really need here), then

while ( 1 ) {
or even
while ( 'infinite' ) {

would be better.

&get_todays_date;

$todays_date = get_todays_date();
if ($date ne $todays_date) {
$date= $todays_date;
do something.....
}
}

sub get_todays_date {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year=$year+1900;
$mon=$mon+1;
if (length($mon) eq 1) {
$mon = "0"."$mon";
^^^^^^

perldoc -q vars

What's wrong with always quoting "$vars"?

So make that:

$mon = '0'. $mon;
or
$mon = "0$mon";

(but using printf/sprintf is Much Better than trying to do it yourself.)

}
if (length($mday) eq 1) {
$mday = "0"."$mday";
}
$todays_date = "$year"."-"."$mon"."-"."$mday";
}


sub get_todays_date {
my($day, $mon, $year) = (localtime)[3,4,5];
return sprintf '%04d-%02d-%02d', $year+1900, $mon+1, $day;
}
 
E

Eric Pozharski

Graham Stow said:
if ($date ne $todays_date) {
$date= $todays_date;
do something.....
}

That "does something" because I<$date> is not equal to I<$todays_date>.

*CUT*

Surely you're interested why condition succedes? To know it you must do
a big deal of cleanup -- insert 2 statements (C<use strict;> and
C<use warnings;>) before any code; then clean your code until perl will
be happy with your attempts; then run. I believe, (dirt guessing here)
sooner or later you'll see warning about something undefined.

p.s. excuse my bad mood, but I think you won't.
 
G

Graham Stow

My logic is that, when the value of <$todays_date> changes, it will because
it will have become tomorrow's date, and will not equal <$date> anymore. So
then assign the value of <$todays_date> to <$date> and do something.
 
J

Jürgen Exner

Graham Stow said:
My logic is that, when the value of <$todays_date> changes, it will because
it will have become tomorrow's date, and will not equal <$date> anymore. So
then assign the value of <$todays_date> to <$date> and do something.

So you want to do something once every day,right?

There are tasks Perl is good at and there are tasks other tools are
custom made for. For this application a cron job seems to be much better
choice.

jue
 
E

Eric Pozharski

Graham Stow said:
My logic is that, when the value of <$todays_date> changes, it will because
it will have become tomorrow's date, and will not equal <$date> anymore. So
then assign the value of <$todays_date> to <$date> and do something.

[ Don't top-post -- people don't like to feel stupid ]

Intentions and code are two different beasts.

Do what you was said to do (twice already) -- use fscking cron.
 
P

Peter J. Holzer

That is likely to waste a boatload of cycles.

Yes, if what the script does once a day is the only thing it does. But
if it provides a useful service continuosly it isn't. (For example it
might be an FCGI script which serves web requests all day long and only
needs to switch log files or dump statistics at the end of each day).

It would probably be better to run it once a minute or so using
whatever job scheduling your OS provides, such as "cron".

If it needs to run only at 00:00, why schedule it every minute?

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top