I know that yesterday (the 21st), I bugged the hell of everyone that was logged on during the afternoon.
I finally understand why using "flock HANDLE, LOCK_EX" is much better than "flock HANDLE, 2".
Now here's my new question:
What I need is for a script to be able to open the SAME file twice, the first time for read, the second for write.
The most important aspect of this is I don't want the file to unlock until the SECOND open command is finished.
I thought that if there was a way to do this, it might be something like this:

open FILE, "file.txt"; # open file for read
flock FILE, LOCK_EX; # lock file
while (<FILE>) { $file .= $_; } # read entire contents of file into $file
## some code to deal with the contents of $file
open FILE, ">file.txt"; # open the same file for write WITHOUT first closing the first open command
flock FILE, LOCK_EX; # lock file
## print some stuff to the file
close FILE; ## close file

Now what I hope the above code does is that when it opens the file for reading (the second open command), it doesn't leave time for another script to open file.txt and deal with it.
Why would I want to do this? Because the contents of file.txt are very important.
If I used "close FILE;" before opening the file for the second time, I'm thinking that another script would be allowed to open the file.

Think of this: If I read the contents of file.txt into the variable $file, and then open the file for reading, this means that I wipe out any info that might have been inserted into the file BEFORE I reopened the file for writing.

So basically what I'm asking is that will file.txt remain locked for the entire piece of code above, or just until I open it again?
I hope this question is not phrased too awkwardly and that my fellow monks will forgive me for yesterday. :-)
(Sorry for not using the <CODE> tag, I am on Lynx right now so I don't have many lines to type on and <BR> tags are not allowed inside a <CODE> tag)


In reply to Flocking by mt2k

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.