Some sort of database with transactional integrity would also be my first thought, but if you need to support multiple users making changes, and you can't change the format of the file (because you're interacting with something else, etc.), there is another way to get pretty close to insuring integrity: use another set of files to allow only one 'user' to change the file at a time.

For example, if a program wants to change the file, it checks that directory for glob( 'flat_file_lock_*' ), and if it finds any files, it adds one to the last file name, and touches that file. If it doesn't find any, it touches 'flat_file_lock_0'. (Yes I know there's a race condition here, but if that's an issue, use a database.)

Then each program just needs to wait until it is the lowest-numbered lock file, make its change, and delete its lock file. Primitive but functional.

Another way would be to write your updates to a pipe, and have a separate process listen on that pipe. (Assuming you're not on Windows, where pipes are scary.)

--
Spring: Forces, Coiled Again!

In reply to Re: How to modify/delete records in a flat-file database? by paulbort
in thread How to modify/delete records in a flat-file database? by JoeJaz

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.