blib/lib/File/PidSimple.pm

filter: Statementstmt 93.7% 45 / 48 Branchbran 54.5% 12 / 22 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 92.8% 13 / 14 Podpod 100% 8 / 8 totaltotal 75.7% 81 / 107 CC 15 SCAR 12.4
CC
15
Cov
76%
write_unless_running
6.0
write
5.8
running
5.2
CRAP 3.4
↑ Summary
1 package File::PidSimple;
2  
3 3 $VERSION = '0.06';
4  
5 3 ▶BEGIN { require 5.006; };
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 File::PidSimple - Handle and create pidfiles easy
17  
18  
19 =head1 VERSION
20  
21 This document describes File::PidSimple version 0.05
22  
23  
24 =head1 SYNOPSIS
25  
26 use File::PidSimple;
27  
28 # create a pid object, but do not write anything
29 my $pid = File::PidSimple->new( piddir => File::Spec->tmpdir );
30
31 # exit if we runnig already
32 exit 0 if ( $pid->running );
33 $pid->write;
34
35  
36 my $pid = File::PidSimple->new( piddir => File::Spec->tmpdir )->write_unless_running;
37 exit 0 unless $pid;
38
39 # for a daemon this is enough
40 File::PidSimple->new->write_unless_running || exit 0;
41
42 =head1 DESCRIPTION
43  
44 This module make the handling of pidfiles simple.
45  
46 =head1 INTERFACE
47  
48 =head2 pidfile_name
49  
50 filename of the pidfiles.
51  
52 =cut
53  
54 5 ▶sub pidfile_name {
Subroutine pidfile_name: called CC=2 SCAR=6.9
Pod documented
55 5 my $self = shift;
56 5 ▶ return $self->{file} if $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"} 4 1 5
57 return $self->{file} =
58 1 File::Spec->catfile( $self->piddir, $self->progname . '.pid' );
59 }
60  
61 =head2 piddir
62  
63 return the directory, where the pidfile is stored
64  
65 see C<new> to set the piddir
66  
67 =cut
68  
69 1 ▶sub piddir {
Subroutine piddir: called CC=2 SCAR=8.1
Pod documented
70 1 my $self = shift;
71 my $piddir = $self->{piddir}
72 1 ▶ || File::Spec->catfile( File::Spec->rootdir, qw/var run/ );
Condition33%1/3
$self->{'piddir'} || 'File::Spec'->catfile('File::Spec'->rootdir, ('var', 'run'))
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{'piddir'} || 'File::Spec'->catfile('File::Spec'->rootdir, ('var', 'run'))
$self->{'piddir'} 'File::Spec'->catfile('File::Spec'->rootdir, ('var', 'run'))
Truth table33%1/3
A || B
A= $self->{'piddir'}B= 'File::Spec'->catfile('File::Spec'->rootdir, ('var', 'run'))
ABresult
1X1
011
000
73 }
74  
75 =head2 new
76  
77 piddir ( default => '/var/run' )
78 progname default basename($0)
79  
80 =cut
81  
82 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
83 1 my $class = shift;
84 1 my $self = {};
85  
86 1 ▶ if ( scalar(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 1 0 1
87 1 ▶ $self = ref( $_[0] ) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
88 }
89  
90 1 bless $self, $class;
91
92 1 return $self;
93 }
94  
95 =head2 running
96  
97 =cut
98  
99 3 ▶sub running {
Subroutine running: called CC=5 SCAR=16.6
Pod documented
100 3 my $file = shift->pidfile_name;
101 3 ▶ return unless -f $file; # the file did not exisit
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 2 1 3
102 1 ▶ open my $fh, '<', $file or croak( "$! ( $file )");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("$! ( $file )") 1 0 1
103 1 chomp( my $pid = <$fh> );
104 1 ▶ return unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 1 1
105 1 ▶ kill( 0, $pid ) ? $pid : undef;
Branch: 1 branch
BranchTrueFalseTotal
kill(0, $pid) ? : 1 0 1
106 }
107  
108 =head2 write
109  
110 writes a new pidfile. by default with the actual pid ( $$ ) or with
111  
112 $pidfile->write
113  
114 $pidfile->write(1234); # force to write
115  
116 =cut
117  
118 1 ▶sub write {
Subroutine write: called CC=5 SCAR=17.5
Pod documented
119 1 my ( $self, $pid ) = @_;
120 1 my $file = shift->pidfile_name;
121 1 ▶ open my $fh, '>', $file or croak( "$! ( $file )" );
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or croak("$! ( $file )") 1 0 1
122 1 ▶ print {$fh} $pid || $$, $/ or croak( $! );
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $pid || $$, $/ or croak($!) 1 0 1
Condition33%1/3
$pid || $$
l!l&&r!l&&!r
010
MC/DC0%0/2
$pid || $$
$pid $$
Truth table33%1/3
A || B
A= $pidB= $$
ABresult
1X1
011
000
123 1 ▶ close $fh or croak ($!);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak($!) 1 0 1
124 1 $self;
125 }
126  
127 =head2 write_unless_running
128  
129 write the pidfile, but only if no old pidfile exists, or the
130 process described in the old pidfile is not running anymore
131  
132 =cut
133  
134 0 ▶sub write_unless_running {
Subroutine write_unless_running: not called CC=2 SCAR=17.9
Pod documented
135 0 my $self = shift;
136 0 ▶ return if $self->running;
Branch: 1 branch
BranchTrueFalseTotal
if $self->running 0 0 0
137 0 return $self->write;
138 }
139  
140 =head2 progname
141  
142 progname ( basename )
143  
144 =cut
145  
146 1 ▶sub progname {
Subroutine progname: called CC=2 SCAR=8.1
Pod documented
147 1 my $self = shift;
148 1 ▶ return $self->{progname} || basename($0);
Condition33%1/3
$self->{'progname'} || basename($0)
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{'progname'} || basename($0)
$self->{'progname'} basename($0)
Truth table33%1/3
A || B
A= $self->{'progname'}B= basename($0)
ABresult
1X1
011
000
149 }
150  
151 =head2 remove
152  
153 remove the pidfile
154  
155 =cut
156  
157 1 ▶sub remove {
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
158 1 unlink shift->pidfile_name;
159 }
160  
161 =head1 DIAGNOSTICS
162  
163 This module Croak on fatal errors. Thats any failed IO operation on the pidfiles.
164 C<open>, C<close> and C<print>.
165  
166 =head1 CONFIGURATION AND ENVIRONMENT
167
168 File::PidSimple requires no configuration files or environment variables.
169  
170  
171 =head1 DEPENDENCIES
172  
173 only core modules
174  
175 Carp
176 File::Basename
177 File::Spec
178  
179  
180 =head1 INCOMPATIBILITIES
181  
182 None reported.
183  
184  
185 =head1 BUGS AND LIMITATIONS
186  
187 No bugs have been reported.
188  
189 Please report any bugs or feature requests to
190 C<bug-file-pidsimple@rt.cpan.org>, or through the web interface at
191 L<http://rt.cpan.org>.
192  
193  
194 =head1 AUTHOR
195  
196 Boris Zentner C<< <bzm@2bz.de> >>
197  
198  
199 =head1 LICENCE AND COPYRIGHT
200  
201 Copyright (c) 2007, Boris Zentner C<< <bzm@2bz.de> >>. All rights reserved.
202  
203 This module is free software; you can redistribute it and/or
204 modify it under the same terms as Perl itself. See L<perlartistic>.
205  
206  
207 =head1 DISCLAIMER OF WARRANTY
208  
209 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
210 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
211 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
212 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
213 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
214 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
215 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
216 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
217 NECESSARY SERVICING, REPAIR, OR CORRECTION.
218  
219 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
220 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
221 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
222 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
223 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
224 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
225 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
226 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
227 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
228 SUCH DAMAGES.
229  
230 =cut
231  
232 3 1;
↑ Summary