blib/lib/Tie/Cacher.pm

filter: Statementstmt 95.7% 272 / 284 Branchbran 92.4% 98 / 106 Conditioncond 62.5% 5 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 28 / 28 Podpod 100% 21 / 21 totaltotal 94.2% 425 / 451 CC 60 SCAR 18.0
CC
60
Cov
94%
delete
12.0
fetch_node
9.6
new
9.1
CRAP 6.1
↑ Summary
1 package Tie::Cacher;
2 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = "0.09";
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use base qw(Tie::Hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Object indices
14 sub TC_HEAD () { 0 };
15 sub TC_NODES () { 1 };
16 sub TC_HIT () { 2 };
17 sub TC_MISSED () { 3 };
18 # We could get the effect of count by using keys, but it would reset
19 # first_key/last_key
20 sub TC_COUNT () { 4 };
21 sub TC_MAX_COUNT() { 5 };
22 sub TC_VALIDATE () { 6 };
23 sub TC_LOAD () { 7 };
24 sub TC_SAVE () { 8 };
25 sub TC_USER_DATA() { 9 };
26  
27 # Node indices
28 sub TC_DATA () { 0 }; # Must be zero (documented accessmethod)
29 sub TC_KEY () { 1 };
30 sub TC_PREVIOUS () { 2 };
31 sub TC_NEXT () { 3 };
32 sub TC_NODE_SIZE() { 4 };
33  
34 # This should effectively give us +inf
35 sub INF () { 1e5000000000 };
36  
37 1 my %attributes = map {($_, 1)} qw(validate load save max_count user_data);
38 52 ▶sub new {
Subroutine new: called CC=9 SCAR=22.1
Pod documented
39 52 ▶ defined(my $class = shift) ||
Branch: 1 branch
BranchTrueFalseTotal
defined(my $class = shift()) or croak("Too few arguments. Usage: Tie::Cacher->new(key-val-pairs)") 52 0 52
40 croak "Too few arguments. Usage: Tie::Cacher->new(key-val-pairs)";
41 52 my $cacher = bless [], $class;
42 52 my $head = [];
43 52 $cacher->[TC_HEAD] = $head;
44 52 $head->[TC_NEXT] = $head;
45 52 $head->[TC_PREVIOUS] = $head;
46 52 $cacher->[TC_HIT] = $cacher->[TC_MISSED] = $cacher->[TC_COUNT] = 0;
47 52 $cacher->[TC_MAX_COUNT] = INF;
48 52 $cacher->[TC_NODES] = {};
49  
50 52 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 6 46 52
51 6 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 6 0 6
52 6 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 2 4 6
53 2 @_ = %{$_[0]};
54 } else {
55 4 @_ = (max_count => $_[0]);
56 }
57 }
58 6 ▶ croak "Odd number of arguments. Usage: $class->new(key-val-pairs)" if
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 6 6
59 @_ %2;
60 }
61 52 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 84 50 134
62 84 my $key = shift;
63 84 ▶ $attributes{$key} || croak "Unknown key $key in $class->new, $class";
Branch: 1 branch
BranchTrueFalseTotal
$attributes{$key} or croak("Unknown key $key in $class->new, $class") 82 2 84
64 82 $cacher->$key(shift);
65 }
66 50 ▶ $cacher->[TC_MAX_COUNT] ||= INF; # Infinity really
Condition50%1/2
$cacher->[5] ||= "Inf"
l!l
500
MC/DC0%0/1
$cacher->[5] ||= "Inf"
$cacher->[5]
Truth table50%1/2
A
A= $cacher->[5]
Aresult
11
00
67 50 return $cacher;
68 }
69  
70 106 ▶sub keys : method {
Subroutine keys: called CC=1 SCAR=0.0
Pod documented
71 106 return CORE::keys %{shift->[TC_NODES]};
72 }
73  
74 328 ▶sub exists : method {
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
75 328 my $cacher = shift;
76 328 return exists $cacher->[TC_NODES]{shift()};
77 }
78  
79 420 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
80 420 return shift->[TC_COUNT]
81 }
82  
83 84 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
84 84 my $cacher = shift;
85  
86 # Make nodes single connected
87 84 $cacher->[TC_NODES] = {};
88 84 my $head = $cacher->[TC_HEAD];
89 84 my $ptr = $head->[TC_PREVIOUS];
90 84 undef $ptr->[TC_NEXT];
91 84 $ptr = $head->[TC_NEXT];
92 84 $head->[TC_NEXT] = $head->[TC_PREVIOUS] = $head;
93 84 $cacher->[TC_COUNT] = 0;
94  
95 84 ▶ while ($ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr) 200551 84 200635
96 # We must remove both the forward and backward links, otherwise
97 # perl will do a recursive free and might run out of stackspace
98 200551 undef $ptr->[TC_PREVIOUS];
99 200551 $ptr = delete $ptr->[TC_NEXT];
100 }
101 }
102  
103 1 *clear = \&DESTROY;
104  
105 # Tie interface aliasas
106 1 *STORE = \&store;
107 1 *FETCH = \&fetch;
108 1 *TIEHASH = \&new;
109 1 *FIRSTKEY = \&first_key;
110 1 *NEXTKEY = \&next_key;
111 1 *EXISTS = \&exists;
112 1 *DELETE = \&delete;
113 1 *CLEAR = \&clear;
114  
115 1 1;
116  
117 __END__
↑ Summary