blib/lib/MYDan/Util/FLock.pm untested

filter: Statementstmt 0.0% 0 / 17 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 37 CC 2 SCAR 11.0
CC
2
Cov
0%
new
6.0
lock
2.0
unlock
2.0
CRAP 3.0
1 package MYDan::Util::FLock;
2  
3 =head1 NAME
4  
5 MYDan::Util::FLock - flock
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::Util::FLock;
10  
11 my $lock = MYDan::Util::FLock->new( '/lock/file' );
12  
13 die "Locked by other processes.\n" unless $lock->lock();
14  
15 $lock->unlock();
16
17 =cut
18  
19 use strict;
20 use warnings;
21 use Carp;
22 use Fcntl qw( :flock );
23  
24 sub new
25 {
26 my ( $class, $file, $self ) = splice @_, 0, 2;
27  
28 confess "open $file: $!" unless open $self->{fh}, '+>', $file;
29  
30 bless $self, ref $class || $class;
31 }
32  
33 sub lock
34 {
35 flock shift->{fh}, LOCK_EX | LOCK_NB;
36 }
37  
38 sub unlock
39 {
40 flock(shift->{fh}, LOCK_UN) or die "Cannot unlock $!\n";
41 }
42
43 1;