File Coverage

blib/lib/File/PidSimple.pm
Criterion Covered Total %
statement 43 46 93.4
branch 12 22 54.5
condition 3 9 33.3
subroutine 13 14 92.8
pod 8 8 100.0
total 79 99 79.8


line stmt bran cond sub pod time code
1             package File::PidSimple;
2              
3             $VERSION = '0.06';
4              
5 2     2   58330 BEGIN { require 5.006; };
6              
7 2     2   13 use warnings;
  2         4  
  2         62  
8 2     2   11 use strict;
  2         4  
  2         69  
9              
10 2     2   15 use File::Spec;
  2         4  
  2         69  
11 2     2   17 use File::Basename qw/basename/;
  2         3  
  2         303  
12 2     2   10 use Carp qw/croak/;
  2         3  
  2         1380  
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             sub pidfile_name {
55 5     5 1 9 my $self = shift;
56 5 100       144 return $self->{file} if $self->{file};
57 1         4 return $self->{file} =
58             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 to set the piddir
66              
67             =cut
68              
69             sub piddir {
70 1     1 1 3 my $self = shift;
71 1   33     10 my $piddir = $self->{piddir}
72             || File::Spec->catfile( File::Spec->rootdir, qw/var run/ );
73             }
74              
75             =head2 new
76              
77             piddir ( default => '/var/run' )
78             progname default basename($0)
79              
80             =cut
81              
82             sub new {
83 1     1 1 149 my $class = shift;
84 1         2 my $self = {};
85              
86 1 50       6 if ( scalar(@_) ) {
87 1 50       6 $self = ref( $_[0] ) ? $_[0] : {@_};
88             }
89              
90 1         4 bless $self, $class;
91            
92 1         3 return $self;
93             }
94              
95             =head2 running
96              
97             =cut
98              
99             sub running {
100 3     3 1 17 my $file = shift->pidfile_name;
101 3 100       132 return unless -f $file; # the file did not exisit
102 1 50       39 open my $fh, '<', $file or croak( "$! ( $file )");
103 1         26 chomp( my $pid = <$fh> );
104 1 50       4 return unless $pid;
105 1 50       35 kill( 0, $pid ) ? $pid : undef;
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             sub write {
119 1     1 1 2 my ( $self, $pid ) = @_;
120 1         4 my $file = shift->pidfile_name;
121 1 50       203 open my $fh, '>', $file or croak( "$! ( $file )" );
122 1 50 33     3 print {$fh} $pid || $$, $/ or croak( $! );
  1         51  
123 1 50       68 close $fh or croak ($!);
124 1         5 $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             sub write_unless_running {
135 0     0 1 0 my $self = shift;
136 0 0       0 return if $self->running;
137 0         0 return $self->write;
138             }
139              
140             =head2 progname
141              
142             progname ( basename )
143              
144             =cut
145              
146             sub progname {
147 1     1 1 4 my $self = shift;
148 1   33     133 return $self->{progname} || basename($0);
149             }
150              
151             =head2 remove
152              
153             remove the pidfile
154              
155             =cut
156              
157             sub remove {
158 1     1 1 438 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, C and C.
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, or through the web interface at
191             L.
192              
193              
194             =head1 AUTHOR
195              
196             Boris Zentner C<< >>
197              
198              
199             =head1 LICENCE AND COPYRIGHT
200              
201             Copyright (c) 2007, Boris Zentner C<< >>. 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.
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             1;