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

Hai all

I am new to PERL. I dont know how to lock a file using perl in multi user environment. ie. when one user is accessing a file it should be locked when other user tries to access it.


Thanks in advance


RUDHRA

Replies are listed 'Best First'.
Re: How do I lock a file in perl?
by saintmike (Vicar) on Sep 07, 2004 at 06:15 UTC
    One thing to keep in mind with flock() is that it doesn't magically "lock" a file. If you flock() a file and another application tries to access it, the operating system will happily allow that.

    The operating system just makes sure that no two flock()s are succeeding on the same file at the same time. Once one flock() succeeds, another flock() on the same file will block (unless nonblocking is selected) until the first one gets released.

    "Locking" is actually a mutual agreement, not a draconic measure one party can impose.

Re: How do I lock a file in perl?
by Elijah (Hermit) on Sep 07, 2004 at 04:54 UTC
Re: How do I lock a file in perl?
by csuhockey3 (Curate) on Sep 07, 2004 at 06:27 UTC
    Have a look at perlfaq for locking files using flock() or how to lock a file 'manually'
Re: How do I lock a file in perl?
by tbone1 (Monsignor) on Sep 07, 2004 at 12:31 UTC
    (SNAPS FINGERS)

    Rats, late again. I've always wanted to tell someone "Oh, flock() it!"

    --
    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

Re: How do I lock a file in perl?
by PerlingTheUK (Hermit) on Sep 07, 2004 at 16:01 UTC
    I personally have started using IO::LockedFile but it is not doing much more then flock() when it comes to locking.

    PerlingTheUK