Buffered/Size.pm

filter: Statementstmt 87.5% 56 / 64 Branchbran 72.7% 16 / 22 Conditioncond 55.5% 10 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 75.0% 9 / 12 Podpod 100% 7 / 7 totaltotal 76.6% 102 / 133 CC 18 SCAR 21.5
CC
18
Cov
79%
new
16.3
write
4.5
read
3.0
CRAP 8.6
↑ Summary Buffered/HTTP.pm »
1 package IO::Buffered::Size;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use base ("IO::Buffered");
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # FIXME: Write documentation
9  
10 9 our $VERSION = '1.00';
11  
12 =head1 NAME
13  
14 IO::Buffered::Size - Size buffering based on pack templates
15  
16 =head1 DESCRIPTION
17  
18 =head1 SYNOPSIS
19  
20 =head1 METHODS
21  
22 =over
23  
24 =cut
25  
26 9 ▶use base "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 9 our @EXPORT_OK = qw();
29  
30 =item new()
31  
32 =cut
33  
34 8 ▶sub new {
Subroutine new: called CC=13 SCAR=27.9
Pod documented
35 8 my ($class, $args, %opts) = @_;
36
37 8 ▶ croak "Args should be an array reference" if ref $args ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args ne "ARRAY" 1 7 8
38  
39 7 my ($template, $offset) = (@{$args}, 0); # Offset defaults to 0
40  
41 # Check if $template is a string, has no * and only returns one number
42 7 ▶ croak "Template should be a string" if !(defined $template and
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template and ref $template eq "" and not $template =~ /^\d+$/ 3 4 7
Condition83%5/6
defined $template and ref $template eq "" and not $template =~ /^\d+$/
!ll&&!rl&&r
214
defined $template and ref $template eq ""
!ll&&!rl&&r
025
MC/DC66%2/3
defined $template and ref $template eq "" and not $template =~ /^\d+$/
defined $template ref $template eq "" not $template =~ /^\d+$/
Truth table75%3/4
A and B and not(C)
A= defined $templateB= ref $template eq ""C= not $template =~ /^\d+$/
ABCresult
0XX0
10X0
1100
1111
43 ref $template eq '' and $template !~ /^\d+$/);
44 4 ▶ croak "Template should not contain *" if $template =~ /\*/;
Branch: 1 branch
BranchTrueFalseTotal
if $template =~ /\*/ 0 4 4
45 4 ▶ croak "Template should only return one number: $template"
Branch: 1 branch
BranchTrueFalseTotal
if ref unpack($template, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") ne "" 0 4 4
46 if ref unpack($template, "x" x 30) ne '';
47
48 # Check if $offset is a number
49 4 ▶ croak "Offset should be a number" if !(defined $offset
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset and $offset =~ /^-?\d+$/ 3 1 4
Condition100%3/3
defined $offset and $offset =~ /^-?\d+$/
!ll&&!rl&&r
121
MC/DC100%2/2
defined $offset and $offset =~ /^-?\d+$/
defined $offset $offset =~ /^-?\d+$/
Truth table100%3/3
A and B
A= defined $offsetB= $offset =~ /^-?\d+$/
ABresult
0X0
100
111
50 and $offset =~ /^-?\d+$/);
51
52 # Check that $regexp is a Regexp or a non empty string
53 croak "Option MaxSize should be a positiv integer" if $opts{MaxSize} and !(
54 1 ▶ $opts{MaxSize} =~ /^\d+$/ and $opts{MaxSize} > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"MaxSize"} and not $opts{"MaxSize"} =~ /^\d+$/ && $opts{"MaxSize"} > 0 0 1 1
Condition16%1/6
$opts{"MaxSize"} and not $opts{"MaxSize"} =~ /^\d+$/ && $opts{"MaxSize"} > 0
!ll&&!rl&&r
100
$opts{'MaxSize'} =~ /^\d+$/ && $opts{'MaxSize'} > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$opts{"MaxSize"} and not $opts{"MaxSize"} =~ /^\d+$/ && $opts{"MaxSize"} > 0
$opts{"MaxSize"} $opts{'MaxSize'} =~ /^\d+$/ $opts{'MaxSize'} > 0
Truth table25%1/4
A and not(B && C)
A= $opts{"MaxSize"}B= $opts{'MaxSize'} =~ /^\d+$/C= $opts{'MaxSize'} > 0
ABCresult
0XX0
1110
10X1
1101
55  
56 my %self = (
57 buffer => '',
58 offset => $offset,
59 minsize => length(pack($template, 0)), # Get minimun size
60 template => $template,
61 maxsize => $opts{MaxSize},
62 1 );
63
64 1 ▶ return bless \%self, (ref $class || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
65 }
66  
67  
68 =item flush($str, ...)
69  
70 =cut
71  
72 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
73 0 my $self = shift;
74 0 $self->{buffer} = join ('', @_);
75 }
76  
77 =item buffer()
78  
79 =cut
80  
81 0 ▶sub buffer {
Subroutine buffer: not called CC=1 SCAR=6.9
Pod documented
82 0 my $self = shift;
83 0 return $self->{buffer};
84 }
85  
86 =item write($str, ...)
87  
88 =cut
89  
90 2 ▶sub write {
Subroutine write: called CC=3 SCAR=15.0
Pod documented
91 2 my $self = shift;
92 2 my $str = join ('', @_);
93
94 2 ▶ if(my $maxsize = $self->{maxsize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $maxsize = $self->{"maxsize"}) 0 2 2
95 0 my $length = length($str) + length($self->{buffer});
96 0 ▶ if($length > $maxsize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > $maxsize) 0 0 0
97 0 croak "Buffer overrun";
98 }
99 }
100  
101 2 $self->{buffer} .= $str;
102 }
103  
104 =item read()
105  
106 =cut
107  
108 4 ▶sub read {
Subroutine read: called CC=3 SCAR=11.0
Pod documented
109 4 my ($self) = (@_);
110 4 my $template = $self->{template};
111 4 my $offset = $self->{offset};
112 4 my $minsize = $self->{minsize};
113 4 my @records;
114  
115 4 ▶ while(length $self->{buffer} > $minsize) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"buffer"} > $minsize) 5 2 7
116 5 my $length = (unpack($template, $self->{buffer}))[0]+$offset;
117 5 my $datastart = length(pack($template, $length));
118
119 5 ▶ if(length $self->{buffer} >= $length + $datastart) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'buffer'} >= $length + $datastart) { } 3 2 5
120 3 push(@records, substr($self->{buffer}, $datastart, $length));
121 3 substr($self->{buffer}, 0, $length+$datastart) = '';
122 } else {
123 2 last;
124 }
125 }
126 4 return @records;
127 }
128  
129 =item returns_last()
130  
131 =cut
132  
133 0 ▶sub returns_last {
Subroutine returns_last: not called CC=1 SCAR=6.9
Pod documented
134 0 return 1;
135 }
136  
137 =item read_last()
138  
139 =cut
140  
141 2 ▶sub read_last {
Subroutine read_last: called CC=2 SCAR=6.9
Pod documented
142 2 my ($self) = @_;
143 2 my @records = $self->read();
144
145 2 my $template = $self->{template};
146 2 my $offset = $self->{offset};
147
148 2 ▶ if($self->{buffer} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buffer"} ne "") 1 1 2
149 1 my $length = (unpack($template, $self->{buffer}))[0]+$offset;
150 1 my $datastart = length(pack($template, $length));
151
152 1 push(@records, substr($self->{buffer}, $datastart));
153 1 $self->{buffer} = '';
154 }
155  
156 2 return @records;
157 }
158  
159 =back
160  
161 =head1 AUTHOR
162  
163 Troels Liebe Bentsen <tlb@rapanden.dk>
164  
165 =head1 COPYRIGHT
166  
167 Copyright(C) 2008 Troels Liebe Bentsen
168  
169 This library is free software; you can redistribute it and/or modify
170 it under the same terms as Perl itself.
171  
172 =cut
173  
174 9 1;
175  
↑ Summary Buffered/HTTP.pm »