EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! I was wondering if there was some perl function that will allow me to wait or pause before doing some command. I have a situation where I can create an infinite LOOP until 100 files are created but that will suck the CPU to the max. I want to avoid this and check if the files exist every 2 mins.

Replies are listed 'Best First'.
Re: pause/wait command?
by ccn (Vicar) on Jul 28, 2004 at 11:40 UTC

      I've never seen it used as the condition of a while loop. I rather like that.

      What I've always done it just inset it in the code

      while (#Files do not yet exist) { sleep(120); #check for files; }
Re: pause/wait command?
by gellyfish (Monsignor) on Jul 28, 2004 at 12:05 UTC

    As an alternative to sleeping if you are on Linux (or indeed Irix) with FAM installed - you might consider using SGI::FAM which will allow your program to get events when the contents of a directory changes:

    use SGI::FAM; my $fam=new SGI::FAM; $fam->monitor('foo'); while (1) { my $event=$fam->next_event; # Blocks print "Pathname: ", $event->filename, " Event: ", $event->type, "\n"; }
    This will report every time a new file is created in the 'foo' directory.

    /J\

      Looks like there is a nice opportunity to do a File::ChangeNotify that presents a unified interface to Win32::ChangeNotify and SGI::FAM (which has got to be one the worst module names i've seen in ages.)


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi


Re: pause/wait command?
by zentara (Cardinal) on Jul 28, 2004 at 14:32 UTC
    Here is a little script using Tk's eventloop to watch a file. You could modify it to watch your directory, or whatever files you want.
    #!/usr/bin/perl use warnings; use strict; use Tk; #read (perldoc -f stat). my $file = shift or die "No file given $!\n"; my $timer; my $lastmtime = (stat $file)[9]; my $mw= tkinit; $mw->withdraw; #make it in background $mw->geometry('+20+20'); #set in upper left corner my $label = $mw->Label( -text => "$file modified at $lastmtime ")->pack(-expand =>1); &startwatchdog($lastmtime); $mw->Button(-text => 'Ok', -command => sub{ $mw->withdraw; $timer->cancel; &startwatchdog( $lastmtime ); })->pack(); MainLoop; sub startwatchdog{ my $mtime = shift; $timer = $mw->repeat(5000, sub { # check every 5 seconds. if ((stat $file)[9] > $lastmtime) { # file was modified. # create your dialog box. $lastmtime = (stat $file)[9]; print "file modified at $lastmtime \n", chr(07); $label->configure(-text =>"$file modified at $lastmtim +e"); $mw->deiconify; $mw->raise; } }); }

    I'm not really a human, but I play one on earth. flash japh
Re: pause/wait command?
by danielcid (Scribe) on Jul 28, 2004 at 12:52 UTC

    You can also use alarm (quite similar to sleep):
    perldoc -f alarm :)

    -DBC
      Alarm is useful if you want to remind yourself to check for the files in two minutes, but do something else in the meanwhile. It's also good for timing out blocking system calls.

      Frankly I don't see how it can be useful here, unless you do something like

      $SIG{ALRM} = 'IGNORE'; alarm 120; sleep; # forever, or until interrupted
      Which is, IMHO, a redundant and less reliable way to get the same effect as
      sleep 120;
      -nuffin
      zz zZ Z Z #!perl