blib/lib/Tie/Cache/Autoupdater.pm

filter: Statementstmt 100% 66 / 66 Branchbran 100% 28 / 28 Conditioncond 100% 10 / 10 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 19 / 19 Podpod n/a totaltotal 100% 129 / 129 CC 19 SCAR 13.9
CC
19
Cov
100%
_fetch
9.0
BEGIN
5.0
TIEHASH
4.0
CRAP 4.0
↑ Summary
1 package Tie::Cache::Autoupdater;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # _now() is the clock timeouts are measured on, best first:
9 # - the monotonic clock: NTP corrections or a manual change of the system
10 # time cannot move it, so a clock set back cannot freeze a cached value;
11 # - Time::HiRes::time, for fractional timeouts without a monotonic clock;
12 # - the built-in time, in whole seconds.
13 # The monotonic clock counts from an arbitrary point (usually boot), so its
14 # readings mean nothing on their own - which is why a fresh key has
15 # last => undef rather than 0.
16 15 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=16.1
17 15 my $hires = eval { require Time::HiRes; 1 };
18  
19 15 my $monotonic;
20 15 ▶ $monotonic = eval {
Branch: 1 branch
BranchTrueFalseTotal
if $hires 14 1 15
21 14 my $id = Time::HiRes::CLOCK_MONOTONIC();
22 14 ▶ Time::HiRes::clock_gettime($id) >= 0 ? $id : undef # -1 on failure
Branch: 1 branch
BranchTrueFalseTotal
&Time::HiRes::clock_gettime($id) >= 0 ? : 13 1 14
23 } if $hires;
24  
25 97 ▶ *_now = defined $monotonic ? sub { Time::HiRes::clock_gettime($monotonic) }
Subroutine __ANON__: called CC=1 SCAR=0.0
26 : $hires ? \&Time::HiRes::time
27 15 ▶ : sub { time };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
defined $monotonic ? : 13 2 15
$hires ? : 1 1 2
28 }
29  
30 15 our $VERSION = '0.23';
31  
32 16 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=4 SCAR=13.9
33 16 my $class = shift;
34 16 my %structure = @_;
35  
36 16 my $self = {};
37 16 ▶ while ( my ($k, $v) = each %structure ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %structure) 16 16 32
38 $self->{ $k } = {
39 timeout => $v->{timeout} || 1,
40 source => $v->{source},
41 last => undef,
42 result => undef,
43 16 ▶ clone => $v->{clone} ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$v->{'clone'} ? : 3 13 16
Condition100%2/2
$v->{'timeout'} || 1
l!l
142
MC/DC100%1/1
$v->{'timeout'} || 1
$v->{'timeout'}
Truth table100%2/2
A
A= $v->{'timeout'}
Aresult
11
00
44 }
45 }
46  
47 16 bless $self, $class;
48 }
49  
50 93 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=6.9
51 93 my ( $self, $k ) = @_;
52  
53 93 ▶ return undef unless exists $self->{ $k };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$k} 2 91 93
54 91 _fetch( $_[0], $k )
55 }
56  
57 18 ▶sub STORE {
Subroutine STORE: called CC=3 SCAR=11.0
58 18 my ( $self, $k, $v ) = @_;
59 $self->{ $k } = {
60 timeout => $v->{timeout} || 1,
61 source => $v->{source},
62 last => undef,
63 result => undef,
64 18 ▶ clone => $v->{clone} ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$v->{'clone'} ? : 1 17 18
Condition100%2/2
$v->{'timeout'} || 1
l!l
171
MC/DC100%1/1
$v->{'timeout'} || 1
$v->{'timeout'}
Truth table100%2/2
A
A= $v->{'timeout'}
Aresult
11
00
65 }
66 }
67  
68 4 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
69 4 keys %{$_[0]};
70 4 scalar each %{$_[0]}
71 }
72  
73 5 ▶sub NEXTKEY { scalar each %{$_[0]} }
Subroutine NEXTKEY: called CC=1 SCAR=0.0
74  
75 3 ▶sub EXISTS { exists $_[0]->{ $_[1] } }
Subroutine EXISTS: called CC=1 SCAR=0.0
76 1 ▶sub DELETE { delete $_[0]->{ $_[1] } }
Subroutine DELETE: called CC=1 SCAR=0.0
77 1 ▶sub CLEAR { %{ $_[0] } = () }
Subroutine CLEAR: called CC=1 SCAR=0.0
78 2 ▶sub SCALAR { scalar %{ $_[0] } }
Subroutine SCALAR: called CC=1 SCAR=0.0
79 1 ▶sub UNTIE { $_[0] = undef }
Subroutine UNTIE: called CC=1 SCAR=0.0
80 15 ▶sub DESTROY { return 1 }
Subroutine DESTROY: called CC=1 SCAR=0.0
81  
82 91 ▶sub _fetch {
Subroutine _fetch: called CC=9 SCAR=22.0
83 91 my ( $self, $k ) = @_;
84  
85 91 ▶ if ( !defined $self->{$k}{last}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{$k}{"last"} or $self->{$k}{"last"} + $self->{$k}{"timeout"} < _now()) 59 32 91
Condition100%3/3
not defined $self->{$k}{"last"} or $self->{$k}{"last"} + $self->{$k}{"timeout"} < _now()
l!l&&r!l&&!r
382132
MC/DC100%2/2
not defined $self->{$k}{"last"} or $self->{$k}{"last"} + $self->{$k}{"timeout"} < _now()
not defined $self->{$k}{"last"} $self->{$k}{"last"} + $self->{$k}{"timeout"} < _now()
Truth table100%3/3
not(A) or B
A= not defined $self->{$k}{"last"}B= $self->{$k}{"last"} + $self->{$k}{"timeout"} < _now()
ABresult
1X1
011
000
86 || $self->{$k}{last} + $self->{$k}{timeout} < _now() ) {
87  
88 # Failure is judged by eval's own value, not by $@: before 5.14 a
89 # destructor that runs eval while the source is dying clears $@, and an
90 # exception object may be false - either way the error would be taken
91 # for an empty result and cached for the whole timeout.
92 59 my @result;
93 59 my $ok = eval {
94 # Before 5.18, calling an undefined source warns "Use of
95 # uninitialized value in subroutine entry" on top of dying.
96 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 59 @result = $self->{$k}{source}->();
98 52 1
99 };
100 59 ▶ unless ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 7 52 59
101 7 my $error = "$@";
102 7 ▶ $error = 'unknown error' unless length $error;
Branch: 1 branch
BranchTrueFalseTotal
unless length $error 2 5 7
103 7 warn qq/Check source subroutine for key $k. Error - $error\n/;
104 return undef
105 7 }
106  
107 52 ▶ if ( @result == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@result == 1) { } 48 4 52
elsif (not @result) { } 1 3 4
108 48 $self->{$k}{result} = $result[0];
109 } elsif ( !@result ) {
110 1 $self->{$k}{result} = undef;
111 } else {
112 3 $self->{$k}{result} = \@result;
113 }
114  
115 52 $self->{$k}{last} = _now();
116 }
117  
118 return $self->{$k}{clone} && ref $self->{$k}{result}
119 ? dclone($self->{$k}{result})
120 84 ▶ : $self->{$k}{result};
Branch: 1 branch
BranchTrueFalseTotal
$self->{$k}{'clone'} && ref $self->{$k}{'result'} ? : 8 76 84
Condition100%3/3
$self->{$k}{"clone"} and ref $self->{$k}{"result"}
!ll&&!rl&&r
7518
MC/DC100%2/2
$self->{$k}{"clone"} and ref $self->{$k}{"result"}
$self->{$k}{"clone"} ref $self->{$k}{"result"}
Truth table100%3/3
A and B
A= $self->{$k}{"clone"}B= ref $self->{$k}{"result"}
ABresult
0X0
100
111
121 }
122  
123 15 1;
124  
125 __END__
↑ Summary