$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";
}
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;
}
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
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.
--
Torvalds' goal for Linux is very simple: World Domination
"Eric Pozharski" <why...@pozharski.name> wrote in message
news:unnvr5x...@carpet.zombinet...
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
[ 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.
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