PaulM
PaulM

Reputation: 375

Perl CGI writing to a file concurrently

Lets say I have the following:

#!/usr/bin/perl
use strict;
use warnings;
use CGI ":standard";

...Snippet...

open (FH, '>', "file.txt") or die ("ERROR:$!");
print FH "something";
close(FH);

As it it cgi on Apache, this cgi script could be called concurrently.

What happens if I wanted conditional logic...

  1. wait until lsof shows file is clear
  2. Read from file
  3. concat with text
  4. write to file

I am investigating utilizing lsof for setting up synchronous file locking, but do not want to go down bad path. (Might be better off using SQL).

Upvotes: 1

Views: 101

Answers (1)

Dave Cross
Dave Cross

Reputation: 69224

  1. Yes, you should almost certainly use a database for this.

  2. If there's some reason why you really don't want to use a database, then at least use the file locking mechanisms that already exist and don't invent your own. There are plenty of questions (and answers) about this in perlfaq5.

Upvotes: 1

Related Questions