File Coverage

blib/lib/Script/NeedsRestart.pm
Criterion Covered Total %
statement 21 23 91.3
branch 5 10 50.0
condition 2 3 66.6
subroutine 6 7 85.7
pod 4 4 100.0
total 38 47 80.8


line stmt bran cond sub pod time code
1             package Script::NeedsRestart;
2              
3 1     1   34549 use warnings;
  1         2  
  1         26  
4 1     1   3 use strict;
  1         1  
  1         230  
5              
6             our $VERSION = '0.02';
7             our $logger;
8             our $SLEEP_BEFORE_RESTART = 2;
9              
10             our @exec_self_cmd = ($^X, (map {'-I' . $_} @INC), $0, @ARGV);
11              
12 3     3   14 sub _log {return $logger;}
13 0     0 1 0 sub set_logger {$logger = $_[1];}
14              
15             sub restart_if_needed {
16 1     1 1 2090 my ($self) = @_;
17              
18 1 50       3 if ($self->check_mtimes) {
19 1         2000112 sleep($SLEEP_BEFORE_RESTART);
20 1         14 $self->restart();
21             }
22              
23 0         0 return;
24             }
25              
26             sub check_mtimes {
27 3     3 1 750 my ($self) = @_;
28              
29 3         155 my @files_to_check = ($0, values(%INC));
30 3         9 foreach my $file (@files_to_check) {
31 158 100 66     3885 if ((-f $file) && (-M $file < 0)) {
32 2 50       9 $self->_log
33             && $self->_log->info('file ' . $file . ' modified');
34 2         24 return $file;
35             }
36             }
37              
38 1         11 return 0;
39             }
40              
41             sub restart {
42 1     1 1 3 my ($self) = @_;
43              
44 1 50       5 $self->_log
45             && $self->_log->debug('exec `' . join(' ', @exec_self_cmd) . '`');
46              
47 1 0         exec(@exec_self_cmd)
48             or die('exec of `' . join(' ', @exec_self_cmd) . '` failed');
49             }
50              
51             1;
52              
53             __END__