[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]
[PATCH] Improved documentation for flock() in perlfunc.pod
G'day p5p,
The example for flock() in perlfunc.pod contains a few errors and
not-so-good practices. The attached patch changes the following:
* We now consistently use scalar filehandles rather than MBOX
* flock() and seek() are checked for errors.
* The SEEK_END constant is used, rather than a magic number.
Cheerio,
Paul
--
Paul Fenwick <pjf@perltraining.com.au> | http://perltraining.com.au/
Director of Training | Ph: +61 3 9354 6001
Perl Training Australia | Fax: +61 3 9354 2681
>From 49330409ac3bc46823d0cbf02865f474ed198126 Mon Sep 17 00:00:00 2001
From: Paul Fenwick <pjf@perltraining.com.au>
Date: Mon, 15 Sep 2008 17:42:41 +1000
Subject: [PATCH] Improved documentation for flock() in perlfunc.pod
* We now consistently use scalar filehandles rather than MBOX
* flock() and seek() are checked for errors.
* The SEEK_END constant is used, rather than a magic number
---
pod/perlfunc.pod | 18 ++++++++++--------
1 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index e5518af..90e6b9b 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -1936,25 +1936,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(MBOX, 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()
--
1.5.2.2
- Follow-Ups from:
-
Roland Giersig <rgiersig@cpan.org>
[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]