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.


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.