blib/lib/Tie/Reduce.pm

filter: Statementstmt 68.2% 43 / 63 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 68.7% 11 / 16 Podpod 100% 5 / 5 totaltotal 68.8% 62 / 90 CC 4 SCAR 4.6
CC
4
Cov
67%
FETCH
2.1
new
2.1
STORE
2.0
CRAP 1.6
↑ Summary
1 1 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Tie::Reduce;
6  
7 1 our $AUTHORITY = 'cpan:TOBYINK';
8 1 our $VERSION = '0.003';
9  
10 ## suppresses warnings
11  
12 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
13 1 my $class = shift;
14 1 my $caller = caller;
15 1 eval "package $caller; our (\$a, \$b)";
16 }
17  
18 ## tie interface
19  
20 1 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=1 SCAR=0.0
21 1 my $class = shift;
22 1 $class->new(@_);
23 }
24  
25 1 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=7.6
26 1 ▶ ref($_[0]) ne __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ne __PACKAGE__ ? : 0 1 1
27 ? $_[0]->get_value
28 : $_[0][0]; # shortcut if not subclassed
29 }
30  
31 4 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=7.0
32 # if subclassed
33 4 ▶ if (ref($_[0]) ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne __PACKAGE__) 0 4 4
34 # take non-optimal route
35 0 my $av = $_[0]->can('assign_value');
36 0 goto $av; # preserve caller
37 }
38
39 4 my ($self) = shift;
40 4 my ($new_value) = @_;
41 4 my ($old_value, $coderef) = @$self;
42  
43 4 my ($caller_a, $caller_b) = do {
44 4 my $pkg = caller();
45 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 \*{$pkg . '::a'}, \*{$pkg . '::b'};
47 };
48 4 local (*$caller_a, *$caller_b);
49
50 4 *$caller_a = \$old_value;
51 4 *$caller_b = \$new_value;
52
53 4 $self->[0] = $coderef->($old_value, $new_value);
54 }
55  
56 ## OO interface
57  
58 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.3
Pod documented
59 1 my $class = shift;
60 1 my ($coderef, $initial_value) = @_;
61 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶ if (ref($coderef) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $coderef ne "CODE") 0 1 1
63 0 require Carp;
64 0 Carp::croak("Expected coderef; got $coderef");
65 }
66 1 bless [$initial_value, $coderef] => $class;
67 }
68  
69 0 ▶sub get_value {
Subroutine get_value: not called CC=1 SCAR=6.9
Pod documented
70 0 $_[0][0];
71 }
72  
73 0 ▶sub set_value {
Subroutine set_value: not called CC=1 SCAR=6.9
Pod documented
74 0 $_[0][0] = $_[1];
75 }
76  
77 0 ▶sub assign_value {
Subroutine assign_value: not called CC=1 SCAR=4.1
Pod documented
78 0 my ($self) = shift;
79 0 my ($new_value) = @_;
80 0 my $old_value = $self->get_value;
81  
82 0 my ($caller_a, $caller_b) = do {
83 0 my $pkg = caller();
84 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 0 \*{$pkg . '::a'}, \*{$pkg . '::b'};
86 };
87 0 local (*$caller_a, *$caller_b);
88
89 0 *$caller_a = \$old_value;
90 0 *$caller_b = \$new_value;
91
92 0 $self->set_value( $self->get_coderef->($old_value, $new_value) );
93 }
94  
95 0 ▶sub get_coderef {
Subroutine get_coderef: not called CC=1 SCAR=6.9
Pod documented
96 0 $_[0][1];
97 }
98  
99 0 ▶sub _set_coderef {
Subroutine _set_coderef: not called CC=1 SCAR=6.9
100 0 $_[0][1] = $_[1];
101 }
102  
103 1 1;
104  
105 __END__
↑ Summary