in reply to (Ovid -flock) Re: File Use
in thread File Use

Only my program will be accessing the file. I just need to make sure it's not being used if someone else runs my program. I'm looking at flock now, but can it return true if the file is in use? qball-"I have node idea?!"

Replies are listed 'Best First'.
(Ovid - flock, part 2 )Re(3): File Use
by Ovid (Cardinal) on Apr 04, 2001 at 23:18 UTC
    Then you're in luck. You want an exclusive lock on the file:
    use Fcntl ':flock'; # import LOCK_* constants my $filename = "somefile.txt"; open FILE, ">>$filename" or die "Cannot open $filename for writing: $! +"; flock FILE, LOCK_EX or die "Cannot get exclusive lock on $filenam +e: $!";
    Just be sure not to forget to unlock the file when done. Plus, my code above is for appending, which is what I suspect you want.


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.