blib/lib/Schedule/ByClock.pm

filter: Statementstmt 17.7% 14 / 79 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 30.7% 4 / 13 Podpod 100% 6 / 6 totaltotal 16.6% 24 / 144 CC 25 SCAR 37.3
CC
25
Cov
12%
_find_next
90.0
get_control_on_minute
56.0
get_control_on_second
30.0
CRAP 41.5
↑ Summary
1 package Schedule::ByClock;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Time::localtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 $VERSION='1.01';
10  
11 ########################################################
12 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
13 0 my $class=shift;
14 0 my $self=[];
15  
16 0 bless($self,$class);
17 0 $self->_init(@_);
18 }
19  
20 ########################################################
21 0 ▶sub get_version {
Subroutine get_version: not called CC=1 SCAR=6.9
Pod documented
22 0 $VERSION;
23 }
24  
25 ########################################################
26 0 ▶sub get_control_list {
Subroutine get_control_list: not called CC=1 SCAR=6.9
Pod documented
27 0 my $self=shift;
28 0 @$self;
29 }
30  
31 ########################################################
32 0 ▶sub set_control_list {
Subroutine set_control_list: not called CC=2 SCAR=17.9
Pod documented
33 0 my $self=shift;
34  
35 0 @$self=();
36 0 foreach ($self->_check_params(@_)) {
37 0 push(@$self,$_);
38 }
39 0 @$self;
40 }
41  
42 ########################################################
43 0 ▶sub get_control_on_second {
Subroutine get_control_on_second: not called CC=5 SCAR=34.0
Pod documented
44 0 my $self=shift;
45 0 my($tm,$until);
46  
47 0 $tm=localtime;
48  
49 0 ▶if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
50 0 $until=$self->_find_next($tm->sec,$self->_check_params(@_));
51 }
52 else {
53 0 $until=$self->_find_next($tm->sec,@$self);
54 }
55 0 ▶return undef unless defined($until);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $until 0 0 0
56  
57 0 ▶if($until>$tm->sec) {
Branch: 2 branches
BranchTrueFalseTotal
if ($until > $tm->sec) { } 0 0 0
elsif ($until < $tm->sec) { } 0 0 0
58 0 sleep($until-$tm->sec);
59 }
60 elsif($until<$tm->sec) {
61 0 sleep($until+60-$tm->sec);
62 }
63 else {
64 0 sleep(60);
65 }
66  
67 0 $until;
68 }
69  
70 ########################################################
71 0 ▶sub get_control_on_minute {
Subroutine get_control_on_minute: not called CC=7 SCAR=40.3
Pod documented
72 0 my $self=shift;
73 0 my($tm,$until,$until_sec);
74  
75 0 $tm=localtime;
76 0 ▶if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
77 0 $until=$self->_find_next($tm->min,$self->_check_params(@_));
78 }
79 else {
80 0 $until=$self->_find_next($tm->min,@$self);
81 }
82 0 ▶return undef unless defined($until);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $until 0 0 0
83  
84 # Syncronize on top of a minute.
85 0 ▶if($tm->sec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tm->sec) 0 0 0
86 0 $until_sec=$self->get_control_on_second(0);
87 }
88  
89 # Which minute?
90 0 $tm=localtime;
91  
92 # Wait until...
93 0 ▶if($until>$tm->min) {
Branch: 2 branches
BranchTrueFalseTotal
if ($until > $tm->min) { } 0 0 0
elsif ($until < $tm->min) { } 0 0 0
94 0 sleep(($until-$tm->min)*60);
95 }
96 elsif($until<$tm->min) {
97 0 sleep(($until+60-$tm->min)*60);
98 }
99 else {
100 # On the minute.
101 0 ▶ return $until if(defined($until_sec)); # We have slept for less than a minute.
Branch: 1 branch
BranchTrueFalseTotal
if defined $until_sec 0 0 0
102 0 sleep(3600); # Wait for a full hour.
103 }
104  
105 0 $until;
106 }
107  
108 ########################################################
109 # 'Internal' subs. Don't call these, since they may,
110 # and will, change without notice.
111 ########################################################
112 0 ▶sub _init {
Subroutine _init: not called CC=2 SCAR=17.9
113 0 my $self=shift;
114  
115 0 foreach ($self->_check_params(@_)) {
116 0 push(@$self,$_);
117 }
118 0 $self;
119 }
120  
121 0 ▶sub _check_params {
Subroutine _check_params: not called CC=5 SCAR=34.0
122 0 my $self=shift;
123 0 my @control;
124  
125 0 foreach (@_) {
126 0 ▶ if(m/\D/||$_<0||$_>59) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\D/ or $_ < 0 or $_ > 59) { } 0 0 0
Condition0%0/6
/\D/ or $_ < 0 or $_ > 59
l!l&&r!l&&!r
000
/\D/ or $_ < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
/\D/ or $_ < 0 or $_ > 59
/\D/ $_ < 0 $_ > 59
Truth table0%0/4
A or B or C
A= /\D/B= $_ < 0C= $_ > 59
ABCresult
1XX1
01X1
0011
0000
127 0 carp("Control value $_ out of bounds")
128 }
129 else {
130 0 push(@control,$_);
131 }
132 }
133 0 @control;
134 }
135  
136 0 ▶sub _find_next {
Subroutine _find_next: not called CC=9 SCAR=45.0
137 0 my ($self,$now,@control)=(@_);
138 0 my ($low,$high,$next);
139  
140 0 ▶return undef unless @control;
Branch: 1 branch
BranchTrueFalseTotal
unless @control 0 0 0
141 0 $low=$high=$control[0]; # Must have an initial value.
142  
143 # Find highest and lowest values in list.
144 0 foreach (@control) {
145 0 ▶ if($_>$high) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ > $high) { } 0 0 0
elsif ($_ < $low) { } 0 0 0
146 0 $high=$_;
147 }
148 elsif($_<$low) {
149 0 $low=$_;
150 }
151 }
152  
153 0 ▶if($now<$high) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now < $high) { } 0 0 0
154 # Search the closest that is higher than now.
155 0 $next=$high;
156 0 ▶ foreach (@control) { $next=$_ if($_>$now&&$_<$next); }
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $now and $_ < $next 0 0 0
Condition0%0/3
$_ > $now and $_ < $next
!ll&&!rl&&r
000
MC/DC0%0/2
$_ > $now and $_ < $next
$_ > $now $_ < $next
Truth table0%0/3
A and B
A= $_ > $nowB= $_ < $next
ABresult
0X0
100
111
157 }
158 else {
159 # Grab low value.
160 0 $next=$low;
161 }
162  
163 0 $next;
164 }
165  
166 1 'Ymer';
167  
168 __END__
↑ Summary