Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How to modify/delete records in a flat-file database?

by wolfi (Scribe)
on Apr 28, 2004 at 17:28 UTC ( #348889=note: print w/replies, xml ) Need Help??


in reply to How to modify/delete records in a flat-file database?

barring any syntax errors:
use Fcntl qw(:DEFAULT :flock); open (MYFILE1, "<", "$file") or sysopen (MYFILE, "$file", O_RDONLY) or die "$!"; flock (MYFILE1, LOCK_SH) or die "$!"; chomp (@input=<MYFILE>); close MYFILE1; # pretending you then did your work on the input, split at the ":" # and stuck the results back into @output... open (MYFILE2, ">", "$file") or sysopen (MYFILE, "$file", O_WRONLY) or die "$!"; flock (MYFILE2, LOCK_EX) or die "$!"; foreach $item(@output){ print MYFILE2 "$item"."\n";} close MYFILE2;

doing the double open-sysopen technique helps avoid races for opening the file and having that content altered in those few miliseconds.

it's rare, if you have a small site, that two users would access the file at the same time - but i wouldn't take the risk. Besides, w/CGI work - if someone is ~intentionally~ messing w/your system -> you want to ensure that you're opening the right data and not something that was swapped in by a nefarious user. (This is the reason for using 2 separate filehandles too -> MYFILE1 and MYFILE2).

LOCK_EX (exclusive locks) are needed, when writing to files - otherwise, LOCK_SH (shared locks) are all that's needed, i believe, when reading.

also: i'm not sure, if i'd actually use DIE like this in a cgi system, if the user is going to see the output. You probably want to 'die' gracefully -> using WARN, &subroutine, or some other action instead.

for more info - see Camel -> pgs 419-422, 571-573, 712, 714-715, 808-810

Replies are listed 'Best First'.
Re: Re: How to modify/delete records in a flat-file database?
by JoeJaz (Monk) on Apr 29, 2004 at 05:58 UTC
    Thanks for your advice and ideas about preventing race conditions. I'll also have to look into that graceful ending of the program. There are a few places in my code where that would be advantageous. Thanks again for looking at my problem. Have a nice night, Joe

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://348889]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2022-12-02 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?