Tutorials Point


  Perl Home

  PERL Functions

© 2013 TutorialsPoint.COM


  Home     References     About TP     Advertising  

PERL flock Function



Advertisements

Syntax

flock FILEHANDLE, OPERATION


Definition and Usage

Supports file locking on the specified FILEHANDLE using the system flock( ), fcntl( ) locking, or lockf( ). The exact implementation used is dependent on what your system supports. OPERATION is one of the static values defined here...

Operation	Result
LOCK_SH 	Set shared lock.
LOCK_EX 	Set exclusive lock.
LOCK_UN 	Unlock specified file.
LONG_NB 	Set lock without blocking.

Return Value

  • 0 on failure to set/unset lock

  • 1 on success to set/unset lock

Example

Following are the usage...

Here's a mailbox appender for BSD systems:

use Fcntl ':flock'; # import LOCK_* constants

    sub lock {
	flock(MBOX,LOCK_EX);
	# and, in case someone appended
	# while we were waiting...
	seek(MBOX, 0, 2);
    }
    sub unlock {
	flock(MBOX,LOCK_UN);
    }
    open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
	    or die "Can't open mailbox: $!";
    lock();
    print MBOX $msg,"\n\n";
    unlock();


Advertisements


  

Advertisements