luc said:
I have a perl program that downloads the value of some shares on the
Brussels stock market. The problem is that every 15 minutes the value
changes. How would I go about changing this program so that it automatically
starts up at 9.30(finishes at 16.00(market closed)) and every 15 minutes
would download the prices and put them in diffrent files, so that I would
have 32 files? With this data you could easily see the flow of the share.
As noted elsewhere in this thread, if the program's not running, it
can't determine if it should be running. You need cron for that, or else
the program has to *be* running, all the time, and just "wake up"
periodically to do its thing.
[Code examples which follow are schematic, not intended to run without
significant modification to suit your local purpose.]
The simple approach:
#!/usr/bin/perl -w
use strict;
while (1) {
# Do your check here.
sleep (15 * 60);
}
This has the advantage of being extremely portable. However, it has the
disadvantage of only ensuring that the *start* of one check will follow
the *end* of the previous check by *at least* 15 minutes. If a download
takes two minutes, then the start of each download will fall 17 minutes
after the last one.
A more complex approach involves setting an "alarm clock" signal to wake
your process:
#!/usr/bin/perl -w
use strict;
use POSIX;
$SIG{ALRM} = \&set_alarm_and_snooze;
set_alarm_and_snooze;
sub set_alarm_and_snooze {
alarm(15 * 60);
# Do your download here.
POSIX:

ause;
}
This approach ensures that the starts of two downloads will be separated
by 15 minutes, but it also has a couple of problems:
1. If a download takes longer than 15 minutes, the next download may
interrupt it.
2. If this script runs for a very long time (e.g., many days), you may
run out of memory, because it's recursive!
These examples are presented to show that a self-contained approach may
not be your best bet. Were this my task, I'd stick with cron -- but a
Windows or other OS may not provide such features (although I've had
success using the Windows task scheduler in XP, and I'm pretty sure that
other contemporary Windows versions have similar things).
Failing that, you will almost certainly end up at CPAN. Some promising
items there include Proc:

aemon, Coro::Timer, Prima::Timer, etc. I've
not used these -- I used the Event package instead, which provides a lot
more functionality but may be complete overkill for you.