On Wed, Nov 26, 2008 at 10:01, Sharan Basappa <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am writing a scheduler for some proprietary task.
> There are two questions pertaining to this
>
> 1) I have to wait for creation of a file by some external process. How
> do I do that in perl?
> In other words, is it possible to list out the files in perl?
>
> 2) If file is not created then I have to wait for sometime. How do I
> put real time delay in perl?
snip

I would say something like

#!/usr/bin/perl

use strict;
use warnings;

#how long to wait before checking again
use constant WAIT => 1;
#number of seconds that must go by with
#no changes to the file for it to be
#considered closed
use constant NO_CHANGE_IN_FILE_FOR_X_SECONDS => 60;

die "usage: $0 file_to_wait_for\n" unless @ARGV == 1;

my $file = shift;

print "looking for $file\n";

#wait for the file to show up
sleep WAIT until -f $file;

print "file exists, waiting to see if it changes\n";

my $time = 0;
my $size = (stat $file)[7];
until ($time == NO_CHANGE_IN_FILE_FOR_X_SECONDS) {
        if ($size == (stat $file)[7]) {
                $time++;
        } else {
                $time = 0;
                $size = (stat _)[7]; #reuse last stat call's buffer
        }
        sleep WAIT;
}

print "file hasn't changed for $time seconds, assuming it is closed\n";

#FIXME: possibly add some code using flock to see if there is an advisory
#lock on the file, but that is only needed if we know that locking is
#being used

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to