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

Hello, I am trying to write a simple webcounter script. My question is; What happens when two different web browsers call the same script that will write to the same file? Will the second stall until the first has completed, or will the second fail when the request fails? Thanks for any help, R

Replies are listed 'Best First'.
Re: Locked files (simple)...
by synistar (Pilgrim) on Sep 03, 2004 at 14:26 UTC
      Thanks,

      In short, flock() has to be used, which waits for exclusive access to a file (for how long, I don't know!).

      Regards, R

      (apologies to Davido for posting in the wrong forum again).
Re: Locked files (simple)...
by davido (Cardinal) on Sep 03, 2004 at 14:25 UTC

    What you speak of is a race condition, and that is one of the primary reasons for which file locking exists. Without proper file locking, both scripts will blindly write at the same time. With proper locking, one will wait for the other to release its lock. This is documented in the POD under perlfaq8: How can I lock a file?.


    Dave

Re: Locked files (simple)...
by tilly (Archbishop) on Sep 03, 2004 at 15:09 UTC
      Thanks tilly,

      Do you know if File::CounterFile is part of Perl? If it is not, does it need installing, or could it be included as a 'used' module that I could hold on my webspace?

      Thanks for any help, R