Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Read/Update File

by rpike (Scribe)
on Aug 25, 2010 at 13:36 UTC ( [id://857193]=note: print w/replies, xml ) Need Help??


in reply to Read/Update File

I appreciate the help but I didn't think I was entering into a spelling bee. All I want to know is what is the best (most efficient, cross OS, generic) way of locking a file (to read in and modify the contents and then write them back to the same file using, if possible, the same filehandle). I don't want to have to read in a file with a lock, modify the contents, close that handle, then open the file again with a lock to write back. This leaves a small window between locks where the file could get read in before another user has his changes saved (the latter user would then overwrite the first user's changes). The application should be able to accomplish this on as many platforms as possible (Windows would come first, Unix, Linux). I'd appreciate any HELPFUL input on this, not posts that simply are used to criticize or weak attempts at trying to impress with their vocab with no helpful insight.

Replies are listed 'Best First'.
Re^2: Read/Update File
by Anonymous Monk on Aug 25, 2010 at 14:43 UTC
      Thank you for the post. Most helpful. :-) I've been trying to use +> to open the file for read/write purposes but cannot read the contents in. I'm trying to read the contents into an array such as (simpler version) but it isn't working :
      open (FH, ">+path"); my @lineArray = <FH>; print join("<br>", @lineArray); # nothing prints although the file do +es have content
      Is there something I have to do with buffering? Reading from the handle in this way is no good when opening for read/write? Any help on this would be most appreciated as well. Thanks.
        > means clobber (overwrite) file , means you get empty file each time, you want +< for read/write. See open
Re^2: Read/Update File
by Argel (Prior) on Aug 25, 2010 at 19:23 UTC
    best (most efficient, cross OS, generic) way of locking a file
    There is no generic solution!! It will be OS and file system dependent. The best you could do is write (or find) a module that accounts for all of these variances. Though, as JavaFan indicated, even that could be challenge since on some systems you will have to isolate the file.

    Elda Taluta; Sarks Sark; Ark Arks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://857193]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found