Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Two things I will recommend:
  • File locking. You have a very big race condition in your code: you read in the contents of the file, alter them, then write them back out. Between the time that you've read the file and you write the file, someone else (ie. another process) could have changed that file. Then you would overwrite that chance when you write the file w/ your file contents.

    One way to fix this is to open the file in read/append mode, flock it, seek to the beginning of the file, read from it, alter the file contents in memory, seek back to the beginning, truncate it, then rewrite the contents from memory. The flock will prevent the race condition (at least w/ another version of your program that uses flock).

    Another way to fix the problem is to use a semaphore file, like in tilly's Simple Locking. Here, you flock a semaphore file when you want to enter the "critical section" of your program, and then other processes of your program cannot enter that critical section until you have released the lock.

    I would recommend the second approach.

  • My second suggestion is, you could just use a DBM file for this, particularly since you already have the notion of keys mapping to values. In particular, you could use MLDBM to serialize the data structure into the DBM format of your choice.


In reply to Re: Learning to use the hash effectively by btrott
in thread Learning to use the hash effectively by Stamp_Guy

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2014-09-23 07:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (211 votes), past polls