LockedFile.pm

filter: Statementstmt 81.4% 88 / 108 Branchbran 54.0% 27 / 50 Conditioncond 47.6% 10 / 21 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 91.3% 21 / 23 Podpod 100% 12 / 12 totaltotal 70.9% 161 / 227 CC 33 SCAR 20.7
CC
33
Cov
70%
print
20.0
truncate
20.0
open
12.3
CRAP 7.9
↑ Summary LockedFile/Flock.pm »
1 package IO::LockedFile;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 $VERSION = 0.23;
7  
8 7 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 @ISA = ("IO::File"); # subclass of IO::File
10  
11 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Set default options
15 7 my %Options;
16 7 _set_option( __PACKAGE__, ( block => 1,
17 lock => 1,
18 scheme => 'Flock',
19 _locked => 0,
20 _writable => 0 ) );
21  
22 ###########################
23 # new
24 ###########################
25 # the constructor
26 39 ▶sub new {
Subroutine new: called CC=8 SCAR=23.2
Pod documented
27 39 my $proto = shift; # get the class name
28 39 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0390
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
29 39 my $self = $class->SUPER::new(); # the object is also file handle
30  
31 # Grab our options (if they're there);
32 39 my $options = {};
33 39 ▶ $options = shift if ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 12 27 39
34  
35 39 ▶ if ( exists $options->{ scheme } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $options->{'scheme'}) { } 0 39 39
elsif ($class eq __PACKAGE__) { } 39 0 39
36 # User-specified scheme (may have to load it)
37 0 $class = join( '::', __PACKAGE__, $options->{ scheme } );
38 0 eval "require $class";
39 0 ▶ croak "Unable to load $class: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
40 }
41 elsif ( $class eq __PACKAGE__ ) {
42 # User didn't specify anything (or subclass), so do it for her
43 39 $class .= '::' . get_scheme( $class );
44 }
45  
46 39 bless ($self, $class);
47  
48 # Store our options
49 39 $self->_set_option( %{ $options } );
50  
51 # if receives any parameters, call our open with those parameters
52 39 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 39 0 39
53 39 ▶ $self->open(@_) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->open(@_) or return undef 30 9 39
54 }
55  
56 30 return $self;
57 } # of new
58  
59 ############################
60 # open
61 ############################
62 39 ▶sub open {
Subroutine open: called CC=10 SCAR=25.1
Pod documented
63 39 my $self = shift;
64  
65 39 my $writable = 0;
66 39 ▶ if ( scalar(@_) == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 23 16 39
elsif ($_[1] =~ /^\d+$/) { } 7 9 16
67 # Perl mode. Look at first character
68  
69 # Quick sanity check. We can't lock a pipe
70 23 ▶ if (( substr( $_[0], 0, 1 ) eq '|' ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq "|" or substr($_[0], -1, 1) eq "|") 0 23 23
Condition33%1/3
substr($_[0], 0, 1) eq "|" or substr($_[0], -1, 1) eq "|"
l!l&&r!l&&!r
0023
MC/DC0%0/2
substr($_[0], 0, 1) eq "|" or substr($_[0], -1, 1) eq "|"
substr($_[0], 0, 1) eq "|" substr($_[0], -1, 1) eq "|"
Truth table33%1/3
A or B
A= substr($_[0], 0, 1) eq "|"B= substr($_[0], -1, 1) eq "|"
ABresult
1X1
011
000
71 ( substr( $_[0], -1, 1 ) eq '|' ) ) {
72 0 croak "Cannot lock a pipe"
73 }
74  
75 # OK, now look at first character
76 23 $writable = substr( $_[0], 0, 1 ) eq '>';
77 }
78 elsif ( $_[1] =~ /^\d+$/ ) {
79 # Numeric mode
80 7 require Fcntl;
81 7 ▶ $writable = ( ( $_[1] & O_APPEND ) ||
Condition33%2/6
$_[1] & 1024 || $_[1] & 64 || $_[1] & 512
l!l&&r!l&&!r
700
$_[1] & 1024 or $_[1] & 64
l!l&&r!l&&!r
070
MC/DC0%0/3
$_[1] & 1024 || $_[1] & 64 || $_[1] & 512
$_[1] & 1024 $_[1] & 64 $_[1] & 512
Truth table25%1/4
A or B || C
A= $_[1] & 1024B= $_[1] & 64C= $_[1] & 512
ABCresult
1XX1
01X1
0011
0000
82 ( $_[1] & O_CREAT ) ||
83 ( $_[1] & O_TRUNC ) );
84 }
85 else {
86 # POSIX mode (we know there were enough parameters since our
87 # SUPER succeeded).
88 9 $writable = ( $_[1] ne 'r' );
89 }
90  
91 39 $self->_set_writable( $writable );
92 # call open of the super class (IO::File) with the rest of the parameters
93 39 ▶ $self->SUPER::open(@_) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::open(@_) or return undef 39 0 39
94  
95 39 ▶ if ( $self->should_lock() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_lock) 39 0 39
96 39 ▶ $self->lock() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->lock or return undef 30 9 39
97 }
98  
99 30 return 1;
100 } # of open
101  
102 ########################
103 # lock
104 ########################
105 30 ▶sub lock {
Subroutine lock: called CC=1 SCAR=0.0
Pod documented
106 30 my $self = shift;
107  
108 30 $self->_set_locked( 1 );
109 30 return 1;
110 } # of lock
111  
112 ########################
113 # unlock
114 ########################
115 30 ▶sub unlock {
Subroutine unlock: called CC=1 SCAR=0.0
Pod documented
116 30 my $self = shift;
117 30 $self->_set_locked( 0 );
118 30 return 1;
119 } # of unlock
120  
121 ########################
122 # close
123 ########################
124 39 ▶sub close {
Subroutine close: called CC=3 SCAR=11.0
Pod documented
125 39 my $self = shift;
126 # if the file was opened - unlock it
127 39 ▶ $self->unlock() if ($self->opened() and $self->have_lock());
Branch: 1 branch
BranchTrueFalseTotal
if $self->opened and $self->have_lock 30 9 39
Condition66%2/3
$self->opened and $self->have_lock
!ll&&!rl&&r
0930
MC/DC50%1/2
$self->opened and $self->have_lock
$self->opened $self->have_lock
Truth table66%2/3
A and B
A= $self->openedB= $self->have_lock
ABresult
0X0
100
111
128 39 $self->SUPER::close();
129 } # of close
130  
131 #######################
132 # have_lock
133 #######################
134 39 ▶sub have_lock {
Subroutine have_lock: called CC=1 SCAR=0.0
Pod documented
135 39 my $self = shift;
136 39 return $self->_get_option( '_locked' );
137 } # of have_lock
138  
139 #######################
140 # _set_locked
141 #######################
142 60 ▶sub _set_locked {
Subroutine _set_locked: called CC=1 SCAR=0.0
143 60 my ( $self, $value ) = @_;
144 60 return $self->_set_option( '_locked', $value );
145 } # of _set_locked
146  
147 #######################
148 # is_writable
149 #######################
150 39 ▶sub is_writable {
Subroutine is_writable: called CC=1 SCAR=0.0
Pod documented
151 39 my $self = shift;
152 39 return $self->_get_option( '_writable' );
153 } # of is_writable
154  
155 #######################
156 # _set_writable
157 #######################
158 39 ▶sub _set_writable {
Subroutine _set_writable: called CC=1 SCAR=0.0
159 39 my ( $self, $value ) = @_;
160 39 return $self->_set_option( '_writable', $value );
161 } # of _set_writable
162  
163 #######################
164 # should_block
165 #######################
166 39 ▶sub should_block {
Subroutine should_block: called CC=1 SCAR=0.0
Pod documented
167 39 my $self = shift;
168 39 return $self->_get_option( 'block' );
169 } # of should_block
170  
171 #######################
172 # should_lock
173 #######################
174 39 ▶sub should_lock {
Subroutine should_lock: called CC=1 SCAR=0.0
Pod documented
175 39 my $self = shift;
176 39 return $self->_get_option( 'lock' );
177 } # of should_lock
178  
179 #######################
180 # print
181 #######################
182 0 ▶sub print {
Subroutine print: not called CC=4 SCAR=30.0
Pod documented
183 0 my ( $self, @args ) = @_;
184  
185 0 my $was_locked = $self->have_lock();
186  
187 0 ▶ if ( ! $was_locked ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($was_locked) 0 0 0
188 0 ▶ return 0 unless $self->lock();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->lock 0 0 0
189 }
190 0 my $rc = $self->SUPER::print( @args );
191 0 ▶ $self->unlock unless $was_locked;
Branch: 1 branch
BranchTrueFalseTotal
unless $was_locked 0 0 0
192  
193 0 return $rc;
194 } # of print
195  
196 #######################
197 # truncate
198 #######################
199 0 ▶sub truncate {
Subroutine truncate: not called CC=4 SCAR=30.0
Pod documented
200 0 my ( $self, @args ) = @_;
201  
202 0 my $was_locked = $self->have_lock();
203  
204 0 ▶ if ( ! $was_locked ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($was_locked) 0 0 0
205 0 ▶ return 0 unless $self->lock();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->lock 0 0 0
206 }
207 0 my $rc = $self->SUPER::truncate( @args );
208 0 ▶ $self->unlock() unless $was_locked;
Branch: 1 branch
BranchTrueFalseTotal
unless $was_locked 0 0 0
209  
210 0 return $rc;
211 } # of truncate
212  
213 #######################
214 # get_scheme
215 #######################
216 46 ▶sub get_scheme {
Subroutine get_scheme: called CC=1 SCAR=0.0
Pod documented
217 46 my $self = shift;
218  
219 46 return _get_option( $self, 'scheme' );
220 } # of get_scheme
221
222 #######################
223 # DESTROY
224 #######################
225 39 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
226 39 my $self = shift;
227 # if the file was opened, close (and unlock) it
228 39 $self->close;
229 } # of DESTROY
230  
231 ######################
232 # _get_option
233 ######################
234 352 ▶sub _get_option {
Subroutine _get_option: called CC=5 SCAR=16.2
235 352 my( $self, $key ) = @_;
236  
237 # Is the option set here?
238 352 ▶ if ( exists $Options{ $self } && exists $Options{ $self }->{ $key } ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $Options{$self} and exists $Options{$self}{$key}) { } 202 150 352
elsif (ref $self) { } 75 75 150
elsif ($self ne __PACKAGE__) { } 75 0 75
Condition100%3/3
exists $Options{$self} and exists $Options{$self}{$key}
!ll&&!rl&&r
7575202
MC/DC100%2/2
exists $Options{$self} and exists $Options{$self}{$key}
exists $Options{$self} exists $Options{$self}{$key}
Truth table100%3/3
A and B
A= exists $Options{$self}B= exists $Options{$self}{$key}
ABresult
0X0
100
111
239 202 return $Options{ $self }->{ $key }
240 }
241 # If we're an object, check out class
242 elsif ( ref( $self ) ) {
243 75 return _get_option( ref( $self ), $key );
244 }
245 # If we're a class other than this one, check defaults
246 elsif ( $self ne __PACKAGE__ ) {
247 75 return _get_option( __PACKAGE__, $key );
248 }
249 # It's nowhere. Probably a typo
250 else {
251 0 croak "Bad option fetch: $key\n";
252 }
253 } # of _get_option
254  
255 ######################
256 # _set_option
257 ######################
258 152 ▶sub _set_option {
Subroutine _set_option: called CC=2 SCAR=6.9
259 152 my( $self, %hash ) = @_;
260  
261 152 ▶ while ( my( $key, $value ) = each %hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %hash) 146 152 298
262 146 $Options{ $self }->{ $key } = $value;
263 }
264 } # of _set_option
265  
266 ######################
267 # import
268 ######################
269 7 ▶sub import {
Subroutine import: called CC=4 SCAR=14.8
270 7 my $pkg = shift;
271 7 my( %config );
272 7 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 7 7
273 0 $config{ scheme } = shift;
274 }
275 else {
276 7 %config = @_;
277 }
278  
279 7 ▶ my $scheme = $config{ scheme } || $pkg->get_scheme;
Condition33%1/3
$config{'scheme'} || $pkg->get_scheme
l!l&&r!l&&!r
070
MC/DC0%0/2
$config{'scheme'} || $pkg->get_scheme
$config{'scheme'} $pkg->get_scheme
Truth table33%1/3
A || B
A= $config{'scheme'}B= $pkg->get_scheme
ABresult
1X1
011
000
280  
281 7 my $class = __PACKAGE__ . "::$scheme";
282 7 eval "require $class";
283 7 ▶ croak "Unable to load $class: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
284  
285 7 $class->_set_option( %config );
286 } # of import
287  
288 7 1;
289 __END__
↑ Summary LockedFile/Flock.pm »