in reply to Flocking .. insanity is close.

Yes, you should use KM's method, the reasons he gives in his comment to the File Locking Tutorial are all valid.

flock-ing a separate file lets you lock several files at once, including files that cannot be locked as you don't have a file handle on them and is generally a cleaner design than locking the file you are using.

Replies are listed 'Best First'.
Re: Re: Flocking .. insanity is close.
by Anonymous Monk on Mar 12, 2001 at 12:47 UTC
    Thank you SO much mirod for the quick reply :-)

    After literally searching everywhere and reading everything I could get my hands on, I finally get it!! :-) Of course thanks to you, perlmonks.org, and KM for the reply to the tutorial.

    Have a wonderful week ... Thank you again

      Sorry to bother anyone again, but please help, this method is still wiping my file clean!? I have done it exactly as the example and it doesn't seem to work.

      It would mean a great deal to me if you could help.

      Thank you so much.

      Here is a small example -- thank you again :)

      #!/usr/local/bin/perl -wT # Flocking test print "Content-type: text/html\n\n"; use Fcntl qw(:flock); my $file = 'test_lock.txt'; my $SEMAPHORE = $file . '.lck'; unless (-e "$file") { open(FILE,">$file") or die "Reason: $!"; print FILE "1"; close(FILE); } open(SEM, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; flock(SEM, LOCK_SH) or die "flock() failed for $SEMAPHORE: $!"; open (FILE, "$file") or die "Can't open $file: $!"; $count = <FILE>; close FILE; sleep 5; close SEM; $count++; open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!"; open (FH, ">$file") or die "Can't open $file: $!"; print FH $count; close FH; sleep 5; close S; print "You have been here <B>$count</B> times!\n";