Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

To flock or lock??

by martymart (Deacon)
on Feb 27, 2003 at 12:25 UTC ( [id://239076]=perlquestion: print w/replies, xml ) Need Help??

martymart has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks

Its a real newbie question, but here goes...
I want to edit raw xml files from a form text area. Opening the valid file, and saving are not a problem. Its for an intranet with very limited users, so I am not so much concerned with security issues. However, I am concerned with the same file being opened by multiple users. Can anyone explain to me why I should (or not) use the flock function. Or would using the get_lock/unlock functions (with a sacrifice file) be the preferred practice ?

Many Thanks,
Martymart.

Replies are listed 'Best First'.
Re: To flock or lock??
by Abigail-II (Bishop) on Feb 27, 2003 at 12:34 UTC
    Since when does Perl have get_lock/unlock functions?

    As for whether or not, and if yes, how, you should use flock depends on what you are exactly doing. Normally you need to flock files if there's a change that if one process is modifying a file, another process is accessing the file (be it for reading or writing). The syntactical details about file locking can be found in perldoc -f flock and in man perlipc.

    Abigail

Re: To flock or lock??
by hardburn (Abbot) on Feb 27, 2003 at 15:04 UTC

    Personally, for any bit of file handling code I write, I always get a shared lock if I'm reading it, and an exclusive lock if I'm writing/appending (unless this is just a quickie script that will never be used a second time). This does introduce a performance hit, but if that becomes an issue, it's a matter of commenting out one line.

    I do this mostly as a means of covering myself. If someone else writes a program that also works with this file, and this file gets messed up because they didn't lock it, I can say: "I did my part. If you didn't do yours, that's not my fault."

    ----
    Reinvent a rounder wheel.

    Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-09-07 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.