Re: Help to infinite loop
by Corion (Patriarch) on Jan 10, 2009 at 22:20 UTC
|
print "checking website...\n";
Otherwise, I wonder what you expect an infinite loop to do, other than making your program run forever... | [reply] [d/l] |
|
Explanation for Corion's proposal: Suffering from Buffering
Perl is happily printing your string into a buffer every minute. Had you waited 3 hours and 35 minutes, you probably would have seen 215 copies of the message appear at once.
| [reply] |
|
Yes, I need it to run until stopped, which I have written an input sub function which changes the variable to 0 which should then stop it. but problem still remains that the loop never runs when its called and locks it up
| [reply] |
|
| [reply] |
Re: Help to infinite loop
by bruno (Friar) on Jan 11, 2009 at 01:32 UTC
|
use Schedule::Cron;
# Create a Cron object and assign a coderef to it
my $cron = new Schedule::Cron( \&check_website );
# Establish the time interval between runs.
# This follows the standard cron notation.
$cron->add_entry("* * * * *");
# Run cron job.
$cron->run();
sub check_website {
warn "Checking website...\n";
# code
}
| [reply] [d/l] |
Re: Help to infinite loop
by dwhite20899 (Friar) on Jan 10, 2009 at 23:19 UTC
|
You don't say what platform you're on, so I can't give you too specific help, but maybe you should look at the alarm function.
http://perldoc.perl.org/functions/alarm.html
instead of sleeping, and calling a sub to stop the loop, write the loop like this (pseudocode):
while (!alarmed) {
yourWorkHere();
alarm("you have 60 sec. to press a key to stop this loop");
if (keyPressed) { alarmed=1; }
}
| [reply] [d/l] [select] |
Re: Help to infinite loop
by Marshall (Canon) on Jan 11, 2009 at 07:00 UTC
|
First, you don't show the code that "checks the website". Maybe something like this will work?
use HTTP::Request::Common qw(GET);
my $url_target = "some_url_to_check"; # like: http//x.y.z.com
while (my $url = GET $url_target)
{
# $url contains some text here that you process...
# this means that the GET function succeeded.
# the GET is a "blocking" dunction and waits until it gets a
# "yes" or "no" answer;
sleep(60); #We don't know how long the last "get" took
#but we are going to wait 60 secs before we
#try it again...
}
else
{
die "Opps there is a problem";
# this will happen maybe about 1/1000 attempts
}
;
I would recommend some sort of control on the number of
trips through the "else" clause to prevent program
infinite loops.
#perhaps like this:
use constant MAX_RETRY => 5;
my $retries =0;
while ( my $url = GET $url_target)
{
#do something here..as above....
$retries =0;
sleep(60);
}
else
{
$retries++;
if ($reties > MAX_RETRY)
{
die "maximum tries exceeded";
}
next; #try again...
}
;
| [reply] [d/l] |
Re: Help to infinite loop
by targetsmart (Curate) on Jan 12, 2009 at 15:21 UTC
|
In these cases you can use perl debugger(man perldebug) to know what is your program really doing.
One more idea would be to use print STDERR Message, where STDERR is unbuffered. | [reply] |
Re: Help to infinite loop
by artist (Parson) on Jan 12, 2009 at 08:42 UTC
|
You might want to use the crontab.
| [reply] [d/l] |