Beefy Boxes and Bandwidth Generously Provided by pair Networks
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 function

by jbert (Priest)
on Nov 03, 2006 at 09:05 UTC ( [id://582028]=note: print w/replies, xml ) Need Help??


in reply to Best practices for modifying a file in place: q's about opening files, file locking, and using the rename function

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:

  1. Converting your existing data to a SQL table
  2. Learning enough SQL to read and update the db (unless you know SQL already)
but then you'll have something which will "just work".

And the SQL isn't hard, and you can play at the sqlite prompt:

$ sqlite3 foo.db SQLite version 3.2.8 Enter ".help" for instructions sqlite> create table players (id integer, name varchar(256), score int +eger); sqlite> insert into players (id, name, score) values (1, "bob", 100); sqlite> insert into players (id, name, score) values (2, "sally", 200) +; sqlite> insert into players (id, name, score) values (3, "frank", 10); sqlite> select * from players; 1|bob|100 2|sally|200 3|frank|10 sqlite> select score from players where name='sally'; 200 sqlite> update players set score=score+10 where name='sally'; sqlite> select score from players where name='sally'; 210 sqlite> delete from players where name='bob'; sqlite> select * from players; 2|sally|210 3|frank|10 sqlite>
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.

  • Comment on Re: Best practices for modifying a file in place: q's about opening files, file locking, and using the rename function
  • Download Code

Replies are listed 'Best First'.
Re^2: Best practices for modifying a file in place: q's about opening files, file locking, and using the rename function
by davebaker (Pilgrim) on Nov 03, 2006 at 15:37 UTC
    Wow-- thanks very much for the link to the article. That's my situation exactly, with respect to the use of a "flat-file database" for a web app-- I need to go to the toolbox and get the right tool instead of using a file.

    I put too much emphasis on the Cookbook's statement that the recipe is the best way for modifying a file in place. Instead of trying to figure out how to add the file locking that the authors recommend to improve the recipe even further, I should have recognized that modifying a file in place is not the right recipe to solve my problem of creating a database that correctly handles updates.

    Thanks for not flogging me <g>

      Good luck with the SQL.

      And, to keep things perlish...

      You may already know all this, but there are a *lot* of perl approaches to accessing databases. At their base, they all use 'DBI'. That defines the interface and DBD::xxx module provides the back-end which talks to the database.

      There are an abundance of modules to layer on top of these if you choose (DBIx::Class, Class::DBI and others), which can avoid you having to actually use SQL. I'm not sure I'd recommend these if your needs are really simple. But have a play and see what suits you best. There is also plenty of DB-related stuff in the perlmonks [id://Tutorials] section.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-25 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found