From: Paul Fenwick Date: Tue, 16 Sep 2008 20:32:39 +0000 (+1000) Subject: Re: [PATCH] Revised: Improved documentation for flock() in perlfunc.pod X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7ed5353dcffcb7d6cb842413f3f29c4dc6074e95;p=p5sagit%2Fp5-mst-13.2.git Re: [PATCH] Revised: Improved documentation for flock() in perlfunc.pod Message-ID: <48CF8B47.30803@perltraining.com.au> p4raw-id: //depot/perl@34378 --- diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index ffd2ab3..8e321ef 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -1935,25 +1935,27 @@ perl. Here's a mailbox appender for BSD systems. - use Fcntl ':flock'; # import LOCK_* constants + use Fcntl qw(:flock SEEK_END); # import LOCK_* and SEEK_END constants sub lock { - flock(MBOX,LOCK_EX); - # and, in case someone appended - # while we were waiting... - seek(MBOX, 0, 2); + my ($fh) = @_; + flock($fh, LOCK_EX) or die "Cannot lock mailbox - $!\n"; + + # and, in case someone appended while we were waiting... + seek($fh, 0, SEEK_END) or die "Cannot seek - $!\n"; } sub unlock { - flock(MBOX,LOCK_UN); + my ($fh) = @_; + flock($fh, LOCK_UN) or die "Cannot unlock mailbox - $!\n"; } open(my $mbox, ">>", "/usr/spool/mail/$ENV{'USER'}") or die "Can't open mailbox: $!"; - lock(); + lock($mbox); print $mbox $msg,"\n\n"; - unlock(); + unlock($mbox); On systems that support a real flock(), locks are inherited across fork() calls, whereas those that must resort to the more capricious fcntl()