Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Help to infinite loop

by Mik0r (Novice)
on Jan 10, 2009 at 22:13 UTC ( #735438=perlquestion: print w/replies, xml ) Need Help??
Mik0r has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am trying to write an infinite loop that will check data on a website every minute. I wrote the sub function to check for the data now I just need to loop it. Below is a modified example to keep it simple of what Ive tried. I have tried some other ways and all the ways I have tried are making the script lock up.

my $sitechecker = 1;
while ($sitechecker) {
print "checking website...";

that is one example of the loop I have been trying to use that will lock the script up. Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Help to infinite loop
by Corion (Pope) on Jan 10, 2009 at 22:20 UTC

    Maybe you want to print a newline?

    print "checking website...\n";

    Otherwise, I wonder what you expect an infinite loop to do, other than making your program run forever...

      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.

      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
        Surely the question still remains, how do you know ...that the loop never runs... ? By locks up..., I assume you mean the caller, aka the script, which if true, surely implies that the loop is indeed starting and as Corion and ikegami have both already pointed out, you (or more accurately, your script) is 'suffering' from buffering.

        A user level that continues to overstate my experience :-))
Re: Help to infinite loop
by bruno (Friar) on Jan 11, 2009 at 01:32 UTC
    Schedule::Cron might also be a good hammer for this nail.
    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 }
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.

    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; } }
Re: Help to infinite loop
by Marshall (Abbot) 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// 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 above.... $retries =0; sleep(60); } else { $retries++; if ($reties > MAX_RETRY) { die "maximum tries exceeded"; } next; #try again... } ;
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.
Re: Help to infinite loop
by artist (Parson) on Jan 12, 2009 at 08:42 UTC
    You might want to use the crontab.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://735438]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2018-06-18 14:25 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.