blib/lib/Tie/Cache/Autoupdater.pm

filter: Statementstmt 66.0% 35 / 53 Branchbran 80.0% 16 / 20 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 50.0% 8 / 16 Podpod n/a totaltotal 64.0% 64 / 100 CC 14 SCAR 12.8
CC
14
Cov
69%
_fetch
7.6
TIEHASH
4.0
STORE
3.0
CRAP 3.6
↑ Summary
1 package Tie::Cache::Autoupdater;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
9 8 eval { require Time::HiRes; };
10 8 ▶ Time::HiRes->import('time') unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 8 0 8
11 }
12  
13 8 our $VERSION = 0.21;
14  
15 8 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=4 SCAR=13.9
16 8 my $class = shift;
17 8 my %structure = @_;
18
19 8 my $self = {};
20 8 ▶ while ( my ($k, $v) = each %structure ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %structure) 4 8 12
21 $self->{ $k } = {
22 timeout => $v->{timeout} || 1,
23 source => $v->{source},
24 last => 0,
25 result => undef,
26 4 ▶ clone => $v->{clone} ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$v->{'clone'} ? : 1 3 4
Condition50%1/2
$v->{'timeout'} || 1
l!l
40
MC/DC0%0/1
$v->{'timeout'} || 1
$v->{'timeout'}
Truth table50%1/2
A
A= $v->{'timeout'}
Aresult
11
00
27 }
28 }
29  
30 8 bless $self, $class;
31 }
32  
33 53 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=7.1
34 53 my ( $self, $k ) = @_;
35  
36 53 ▶ return undef unless exists $self->{ $k };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$k} 0 53 53
37 53 _fetch( $_[0], $k )
38 }
39  
40 9 ▶sub STORE {
Subroutine STORE: called CC=3 SCAR=11.1
41 9 my ( $self, $k, $v ) = @_;
42 $self->{ $k } = {
43 timeout => $v->{timeout} || 1,
44 source => $v->{source},
45 last => 0,
46 result => undef,
47 9 ▶ clone => $v->{clone} ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$v->{'clone'} ? : 1 8 9
Condition50%1/2
$v->{'timeout'} || 1
l!l
90
MC/DC0%0/1
$v->{'timeout'} || 1
$v->{'timeout'}
Truth table50%1/2
A
A= $v->{'timeout'}
Aresult
11
00
48 }
49 }
50  
51 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
52 0 keys %{$_[0]};
53 0 my ( $k, $v ) = each %{$_[0]};
54 0 _fetch( $_[0], $k )
55 }
56  
57 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
58 0 my ( $k, $v ) = each %{$_[0]};
59 0 _fetch( $_[0], $k )
60 }
61  
62 0 ▶sub EXISTS { exists $_[0]->{ $_[1] } }
Subroutine EXISTS: not called CC=1 SCAR=6.9
63 0 ▶sub DELETE { delete $_[0]->{ $_[1] } }
Subroutine DELETE: not called CC=1 SCAR=6.9
64 0 ▶sub CLEAR { %{ $_[0] } = () }
Subroutine CLEAR: not called CC=1 SCAR=6.9
65 0 ▶sub SCALAR { scalar %{ $_[0] } }
Subroutine SCALAR: not called CC=1 SCAR=6.9
66 0 ▶sub UNTIE { $_[0] = undef }
Subroutine UNTIE: not called CC=1 SCAR=6.9
67 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
68  
69 53 ▶sub _fetch {
Subroutine _fetch: called CC=7 SCAR=20.3
70 53 my ( $self, $k ) = @_;
71
72 53 ▶ if ( $self->{$k}{last} + $self->{$k}{timeout} < time() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$k}{"last"} + $self->{$k}{"timeout"} < &Tie::Cache::Autoupdater::time()) 29 24 53
73  
74 29 my @result = eval { $self->{$k}{source}->() };
75 29 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 29 29
76 0 warn qq/Check source subroutine for key $k. Error - $@\n/;
77 return undef
78 0 }
79  
80 29 ▶ if ( @result == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@result == 1) { } 27 2 29
elsif (not @result) { } 0 2 2
81 27 $self->{$k}{result} = $result[0];
82 } elsif ( !@result ) {
83 0 $self->{$k}{result} = undef;
84 } else {
85 2 $self->{$k}{result} = \@result;
86 }
87  
88 29 $self->{$k}{last} = time();
89 }
90
91 return $self->{$k}{clone} && ref $self->{$k}{result}
92 ? dclone($self->{$k}{result})
93 53 ▶ : $self->{$k}{result};
Branch: 1 branch
BranchTrueFalseTotal
$self->{$k}{'clone'} && ref $self->{$k}{'result'} ? : 6 47 53
Condition66%2/3
$self->{$k}{"clone"} and ref $self->{$k}{"result"}
!ll&&!rl&&r
4706
MC/DC50%1/2
$self->{$k}{"clone"} and ref $self->{$k}{"result"}
$self->{$k}{"clone"} ref $self->{$k}{"result"}
Truth table66%2/3
A and B
A= $self->{$k}{"clone"}B= ref $self->{$k}{"result"}
ABresult
0X0
100
111
94 }
95  
96 8 1;
97  
98 __END__
↑ Summary