Buffered/FixedSize.pm

filter: Statementstmt 82.9% 39 / 47 Branchbran 66.6% 8 / 12 Conditioncond 33.3% 4 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 75.0% 9 / 12 Podpod 100% 7 / 7 totaltotal 70.1% 68 / 97 CC 11 SCAR 15.9
CC
11
Cov
72%
new
10.9
write
4.5
flush
2.0
CRAP 4.9
« Buffered/Regexp.pm ↑ Summary Buffered/Last.pm »
1 package IO::Buffered::FixedSize;
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::FixedSize - Fixed size buffering
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 4 ▶sub new {
Subroutine new: called CC=7 SCAR=23.8
Pod documented
35 4 my ($class, $length, %opts) = @_;
36
37 # Check if $length is a number
38 4 ▶ croak "FixedSize should be a number" if !(defined $length
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length and $length =~ /^\d+$/ 3 1 4
Condition66%2/3
defined $length and $length =~ /^\d+$/
!ll&&!rl&&r
031
MC/DC50%1/2
defined $length and $length =~ /^\d+$/
defined $length $length =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $lengthB= $length =~ /^\d+$/
ABresult
0X0
100
111
39 and $length =~ /^\d+$/);
40  
41 # Check that $regexp is a Regexp or a non empty string
42 croak "Option MaxSize should be a positiv integer" if $opts{MaxSize} and !(
43 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
44  
45 my %self = (
46 buffer => '',
47 length => $length,
48 maxsize => $opts{MaxSize},
49 1 );
50
51 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
52 }
53  
54  
55 =item flush($str, ...)
56  
57 =cut
58  
59 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
60 0 my $self = shift;
61 0 $self->{buffer} = join ('', @_);
62 }
63  
64 =item buffer()
65  
66 =cut
67  
68 0 ▶sub buffer {
Subroutine buffer: not called CC=1 SCAR=6.9
Pod documented
69 0 my $self = shift;
70 0 return $self->{buffer};
71 }
72  
73 =item write($str, ...)
74  
75 =cut
76  
77 2 ▶sub write {
Subroutine write: called CC=3 SCAR=15.0
Pod documented
78 2 my $self = shift;
79 2 my $str = join ('', @_);
80
81 2 ▶ if(my $maxsize = $self->{maxsize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $maxsize = $self->{"maxsize"}) 0 2 2
82 0 my $length = length($str) + length($self->{buffer});
83 0 ▶ if($length > $maxsize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > $maxsize) 0 0 0
84 0 croak "Buffer overrun";
85 }
86 }
87  
88 2 $self->{buffer} .= $str;
89 }
90  
91 =item read()
92  
93 =cut
94  
95 4 ▶sub read {
Subroutine read: called CC=2 SCAR=6.9
Pod documented
96 4 my ($self) = (@_);
97 4 my $length = $self->{length};
98  
99 4 my @records;
100 4 ▶ while($length <= length($self->{buffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= length $self->{"buffer"}) 3 4 7
101 3 push(@records, substr($self->{buffer}, 0, $length));
102 3 substr($self->{buffer}, 0, $length) = '';
103 }
104 4 return @records;
105 }
106  
107 =item returns_last()
108  
109 =cut
110  
111 0 ▶sub returns_last {
Subroutine returns_last: not called CC=1 SCAR=6.9
Pod documented
112 0 return 1;
113 }
114  
115 =item read_last()
116  
117 =cut
118  
119 2 ▶sub read_last {
Subroutine read_last: called CC=2 SCAR=6.9
Pod documented
120 2 my ($self) = @_;
121 2 my @records = $self->read();
122 2 ▶ push(@records, $self->{buffer}) if $self->{buffer} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"buffer"} ne "" 1 1 2
123 2 $self->{buffer} = '';
124 2 return @records;
125 }
126  
127  
128 =back
129  
130 =head1 AUTHOR
131  
132 Troels Liebe Bentsen <tlb@rapanden.dk>
133  
134 =head1 COPYRIGHT
135  
136 Copyright(C) 2008 Troels Liebe Bentsen
137  
138 This library is free software; you can redistribute it and/or modify
139 it under the same terms as Perl itself.
140  
141 =cut
142  
143 9 1;
144  
« Buffered/Regexp.pm ↑ Summary Buffered/Last.pm »