maleteen2000, yes, people gave you grief yesterday for your excessive questions in the Chatterbox. While, on one hand, it gets annoying that you keep asking basic questions that are already answered all over this site and others (in fact, the DBI questions you were asking yesterday were dealt with in a post ... yesterday), on the other hand, I do have to commend you on your perserverence in the face of adversity. I'll answer your question, but I'll also list a few resources that you can use to look things up on your own. First, I assume that the above code is pseudocode, because it's terrible. I'll give you the benefit of the doubt. Second, what you really want to do (at least, as far as I can tell from your example), is to modify a file in place with update mode ("+<").
# always test for failure open (FILEHANDLE, "+< $filename") || die "Can't open $filename in upda +te mode: $!\n"; # read entire file into array of lines @array = <FILEHANDLE>; # do stuff to the file here # go back to beginning of file handle seek(FILEHANDLE, 0, 0) or die ("Seek failed on $filename: $!\n"); print FILEHANDLE @array or die ("Print failed on $filename: $!\n"); # truncate the file so we don't have excess garbage at the end truncate(FILEHANDLE, tell(FILEHANDLE)) or die ("Truncate failed on $fi +lename: $!\n"); close (FILEHANDLE) or die ("Close failed on $filename: $!\n");
This code allows you to open the file, read its contents, modify them, and write them back out without having to close and reopen. I haven't tested it with flocking, so you'll need to play with it to see what type of flock works.

Please note that flocking a file doesn't stop others from playing with it. They can still modify the file if they don't check to see if it's flocked.

Further resources:

Please don't take this as a flame, but people will be much more willing to help you if it looks like you've done your homework first.

Cheers!

PS: I took the above code straight from the Perl Cookbook. Beg your parents for a copy.


In reply to Re: Flocking by Ovid
in thread 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.