lib/IPC/Pidfile.pm

filter: Statementstmt 95.2% 40 / 42 Branchbran 50.0% 11 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 6 totaltotal 71.7% 61 / 85 CC 13 SCAR 12.9
CC
13
Cov
78%
pidfile_lock
5.8
pidfile_clear
4.6
pidfile_read
4.1
CRAP 3.6
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package IPC::Pidfile;
4 1 $IPC::Pidfile::VERSION = '0.02';
5 # ABSTRACT: run only one instance of a program at a time
6 1 our $DEBUG = $ENV{IPC_PIDFILE_DEBUG};
7 1 our $PID = pid();
8  
9 # signal handler
10 0 ▶$SIG{TERM} = $SIG{INT} = sub { exit(0) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
11  
12 1 pidfile_lock();
13  
14 1 ▶END { pidfile_clear() }
Subroutine END: called CC=1 SCAR=0.0
15  
16  
17 6 ▶sub pidfile_name { "$0.pid" }
Subroutine pidfile_name: called CC=1 SCAR=0.0
Pod undocumented
18  
19 sub pidfile_lock
20 1 ▶{
Subroutine pidfile_lock: called CC=5 SCAR=17.5
Pod undocumented
21 1 ▶ print "$PID pidfile_lock\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Pidfile::DEBUG 0 1 1
22 1 ▶ die "$0 is already running\n" if (-e pidfile_name()) && pidfile_is_fresh();
Branch: 1 branch
BranchTrueFalseTotal
if -e pidfile_name() and pidfile_is_fresh() 0 1 1
Condition33%1/3
-e pidfile_name() and pidfile_is_fresh()
!ll&&!rl&&r
100
MC/DC0%0/2
-e pidfile_name() and pidfile_is_fresh()
-e pidfile_name() pidfile_is_fresh()
Truth table33%1/3
A and B
A= -e pidfile_name()B= pidfile_is_fresh()
ABresult
0X0
100
111
23 1 ▶ open my $pidfile, '>', pidfile_name() or die 'Unable to open pidfile ' . pidfile_name();
Branch: 1 branch
BranchTrueFalseTotal
open my $pidfile, ">", pidfile_name() or die 'Unable to open pidfile ' . pidfile_name() 1 0 1
24 1 select((select($pidfile), $| = 1)[0]);
25 1 print $pidfile pid();
26 1 close $pidfile;
27 1 return 1;
28 }
29  
30 sub pidfile_is_fresh
31 1 ▶{
Subroutine pidfile_is_fresh: called CC=3 SCAR=12.0
Pod undocumented
32 1 ▶ print "$PID pidfile_is_stale\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Pidfile::DEBUG 0 1 1
33 # kill isnt portable
34 1 ▶ if ($^O ne 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 1 0 1
35 {
36 1 my $pid = pidfile_read();
37 1 return kill 0, $pid;
38 }
39 0 return 0;
40 }
41  
42 sub pid
43 4 ▶{
Subroutine pid: called CC=1 SCAR=0.0
Pod undocumented
44 4 my $pid = $$;
45 # Win32 pids have a trailing newline?!
46 4 chomp $pid;
47 4 return $pid;
48 }
49  
50 sub pidfile_read
51 3 ▶{
Subroutine pidfile_read: called CC=4 SCAR=14.2
Pod undocumented
52 3 ▶ print "$PID pidfile_read\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Pidfile::DEBUG 0 3 3
53 3 ▶ open my $pidfile, '<', pidfile_name() or die 'Unable to open pidfile ' . pidfile_name();
Branch: 1 branch
BranchTrueFalseTotal
open my $pidfile, "<", pidfile_name() or die 'Unable to open pidfile ' . pidfile_name() 3 0 3
54 3 my $pid = do { local($/);<$pidfile> };
55 3 close $pidfile;
56 3 chomp $pid;
57  
58 # avoid negative pid numbers and garbage
59 3 ▶ die "pidfile appears corrupted: $pid\n" unless $pid =~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid =~ /^[0-9]+$/ 0 3 3
60 3 return $pid;
61 }
62  
63 sub pidfile_clear
64 1 ▶{
Subroutine pidfile_clear: called CC=4 SCAR=15.2
Pod undocumented
65 1 ▶ print "$PID pidfile_clear\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Pidfile::DEBUG 0 1 1
66 1 ▶ if (pid() eq pidfile_read())
Branch: 1 branch
BranchTrueFalseTotal
if (pid() eq pidfile_read()) 1 0 1
67 {
68 1 ▶ unlink pidfile_name() or die "Unable to delete pidfile " . pidfile_name();
Branch: 1 branch
BranchTrueFalseTotal
unlink pidfile_name() or die 'Unable to delete pidfile ' . pidfile_name() 1 0 1
69 }
70 }
71 1 1;
72  
73 __END__
↑ Summary