Hello kiat,

I'm sure the other monks will let me know if I am on the wrong track, but I think you may have a problem in the line flock(FILE, 2) || die $! if $flock;

By putting in the phrase "if $flock" what you are saying is that the script should die if the lock is "shared for reading" but not if it is not locked at all! What I think would be preferable is simply:

flock(FILE, 2) || die "can't print to $tmp_file: $!";
as this would force the script to die on any circumstances other than a "locked for writing" situation.

Addendum: It also seems a little strange to me that you go to the trouble of defining a variable $flock but then use flock(FILE, 2). Though I can see that you wouldn't want to mistakenly do anything else but "2" in a writing situation I would think that consistent use of one or other methods (and making sure that usage is correct in the chosen method) is better than being inconsistent and inviting anomolies to occur.

In reply to Re: Re: Re: Text file got corrupted? by jonnyfolk
in thread Text file got corrupted? by kiat

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.