Perl Monk, Perl Meditation | |
PerlMonks |
Re: Best practices for modifying a file in place: q's about opening files, file locking, and using the rename functionby jbert (Priest) |
on Nov 03, 2006 at 09:05 UTC ( [id://582028]=note: print w/replies, xml ) | Need Help?? |
It's nowhere near as bad as the Pounding a nail, shoe or glass bottle article, but I am reminded of it a little. However, I would respectfully suggest that you consider using a different tool for this job, especially since you say the file is important.
The tool which seems most suitable to me is SQLite - it essentially offers all (most) of the features of a server-based database, but just works on a file like you have - doing all the locking and concurrency you need, on Unix/Linux or Windows. The overhead for you will be:
And the SQL isn't hard, and you can play at the sqlite prompt: There is a little trickery in the update command. Rather than read the info in one SQL SELECT and then store it as a seperate UPDATE, I did the read+update in one statement. This is because another process could have got in and altered the score between my read and my update. There is no chance of that happening if done with one statement. If you want to get deeper into SQL you can get around this with transactions and/or locks, but the above is probably be all you need. You can get very complicated with SQL if you need/want to, and in fact I've stuck in a (redundant?) id number in there out of habit - which I could have told the system was an indexed, unique key. But all of that stuff is really for bigger systems where you're giving hints to help performance and similar. If you're currently using a flat file you're probably not near worrying about that yet.
In Section
Seekers of Perl Wisdom
|
|