blib/lib/Tie/Scalar/RingBuffer.pm

filter: Statementstmt 93.1% 54 / 58 Branchbran 58.3% 21 / 36 Conditioncond 50.0% 7 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 84.6% 11 / 13 Podpod 100% 3 / 3 totaltotal 72.9% 97 / 133 CC 25 SCAR 19.2
CC
25
Cov
76%
TIESCALAR
13.2
redo
6.0
increment
5.8
CRAP 6.8
↑ Summary
1 package Tie::Scalar::RingBuffer;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our $VERSION = '0.04';
8  
9 1 ▶sub _castbool ($) { $_[0] ? 1 : 0 }
Subroutine _castbool: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 0 1
10  
11 8 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=12 SCAR=25.8
12 8 my $class = shift;
13 8 my $list = shift;
14 8 ▶ my $opts = shift || +{};
Condition100%2/2
shift() || {}
l!l
62
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
15  
16 8 ▶ croak "Tie::Scalar::RingBuffer expects a listref" unless @_ == 0 && ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 0 and ref $list eq "ARRAY" 0 8 8
Condition33%1/3
@_ == 0 and ref $list eq "ARRAY"
!ll&&!rl&&r
008
MC/DC0%0/2
@_ == 0 and ref $list eq "ARRAY"
@_ == 0 ref $list eq "ARRAY"
Truth table33%1/3
A and B
A= @_ == 0B= ref $list eq "ARRAY"
ABresult
0X0
100
111
17 8 ▶ croak "Tie::Scalar::RingBuffer expects a hashref of options" unless ref($opts) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 0 8 8
18  
19 # validate options
20 8 for (keys %$opts){
21 5 ▶ /^(?:start_offset|increment|random)$/ or croak "Unrecognized option '$_'";
Branch: 1 branch
BranchTrueFalseTotal
/^(?:start_offset|increment|random)$/ or croak("Unrecognized option '${_}'") 5 0 5
22 }
23  
24 8 ▶ if (exists($opts->{'increment'}) && exists($opts->{'random'})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"increment"} and exists $opts->{"random"}) 0 8 8
Condition66%2/3
exists $opts->{"increment"} and exists $opts->{"random"}
!ll&&!rl&&r
620
MC/DC0%0/2
exists $opts->{"increment"} and exists $opts->{"random"}
exists $opts->{"increment"} exists $opts->{"random"}
Truth table66%2/3
A and B
A= exists $opts->{"increment"}B= exists $opts->{"random"}
ABresult
0X0
100
111
25 0 croak "options 'increment' and 'random' are mutually exclusive"
26 }
27  
28 # set defaults
29 8 my $self = bless +{ _list => $list,
30 _ix => 0,
31 _last_ix => 0,
32 _random => 0,
33 _debug => 0,
34 _incr => 1}, $class;
35  
36 # apply options
37 8 ▶ if (exists $opts->{'random'}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"random"}) 1 7 8
38 1 $self->{_random} = _castbool ($opts->{'random'});
39 1 $self->start_offset(int rand @{$self->{_list}});
40 }
41 8 ▶ $self->start_offset ($opts->{'start_offset'}) if exists $opts->{'start_offset'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"start_offset"} 2 6 8
42 8 ▶ $self->increment ($opts->{'increment'}) if exists $opts->{'increment'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"increment"} 2 6 8
43  
44 8 $self
45 }
46  
47 sub FETCH
48 83 ▶{
Subroutine FETCH: called CC=3 SCAR=11.0
49 83 my $self = shift;
50 83 ▶ print ("FETCH(ix=",$self->{_ix},")\n") if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 83 83
51 83 my ($list,$ix) = ($self->{_list}, $self->{_ix});
52 $self->{_ix} = $self->{_random} ? (rand @$list)
53 83 ▶ : ($ix + $self->{_incr}) % @$list;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_random'} ? : 10 73 83
54 83 $self->{_last_ix} = $ix;
55 83 $list->[$ix];
56 }
57  
58 sub redo ()
59 0 ▶{
Subroutine redo: not called CC=2 SCAR=17.9
Pod documented
60 0 my $self = shift;
61 0 ▶ croak "redo expects an object" unless UNIVERSAL::isa($self, __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($self, __PACKAGE__) 0 0 0
62 0 $self->{_list}->[$self->{_last_ix}]
63 }
64  
65 sub STORE
66 3 ▶{
Subroutine STORE: called CC=2 SCAR=7.1
67 3 my $self = shift;
68 3 ▶ print("STORE(_last_ix=",$self->{_last_ix},")\n") if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 3 3
69 3 $self->{_list}->[$self->{_last_ix}] = shift;
70 }
71  
72 7 ▶sub UNTIE { }
Subroutine UNTIE: called CC=1 SCAR=0.0
73 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
74  
75 3 ▶sub start_offset {
Subroutine start_offset: called CC=5 SCAR=17.3
Pod documented
76 3 my $self = shift;
77 3 ▶ croak "start_offset() expects an object" unless UNIVERSAL::isa($self,__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($self, __PACKAGE__) 0 3 3
78  
79 3 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
80 3 my $start_offset = shift;
81 3 ▶ croak "start_offset() expects a numeric offset" unless defined($start_offset) && $start_offset =~ /^[+-]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start_offset and $start_offset =~ /^[+-]?\d+$/ 0 3 3
Condition33%1/3
defined $start_offset and $start_offset =~ /^[+-]?\d+$/
!ll&&!rl&&r
003
MC/DC0%0/2
defined $start_offset and $start_offset =~ /^[+-]?\d+$/
defined $start_offset $start_offset =~ /^[+-]?\d+$/
Truth table33%1/3
A and B
A= defined $start_offsetB= $start_offset =~ /^[+-]?\d+$/
ABresult
0X0
100
111
82 3 $self->{_ix} = ($start_offset % scalar @{$self->{_list}});
83 }
84 3 $self->{_ix};
85 }
86  
87 2 ▶sub increment {
Subroutine increment: called CC=5 SCAR=17.5
Pod documented
88 2 my $self = shift;
89 2 ▶ croak "increment() expects an object" unless UNIVERSAL::isa($self,__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($self, __PACKAGE__) 0 2 2
90 2 ▶ if(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
91 2 my $incr = shift;
92 2 ▶ croak "increment() expects an integer" unless defined($incr) && $incr =~ /^[+-]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $incr and $incr =~ /^[+-]?\d+$/ 0 2 2
Condition33%1/3
defined $incr and $incr =~ /^[+-]?\d+$/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $incr and $incr =~ /^[+-]?\d+$/
defined $incr $incr =~ /^[+-]?\d+$/
Truth table33%1/3
A and B
A= defined $incrB= $incr =~ /^[+-]?\d+$/
ABresult
0X0
100
111
93 2 $self->{_incr} = $incr;
94 }
95 2 $self->{_incr};
96 }
97  
98  
99 1 1;
100 __END__
↑ Summary