Buffered/Regexp.pm

filter: Statementstmt 77.7% 42 / 54 Branchbran 77.7% 14 / 18 Conditioncond 60.0% 12 / 20 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 75.0% 9 / 12 Podpod 100% 7 / 7 totaltotal 71.0% 86 / 121 CC 17 SCAR 19.5
CC
17
Cov
74%
new
14.0
read
4.8
write
3.1
CRAP 7.0
« Buffered/HTTP.pm ↑ Summary Buffered/FixedSize.pm »
1 package IO::Buffered::Regexp;
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 9 our $VERSION = '1.00';
9  
10 # FIXME: Write documentation
11  
12 =head1 NAME
13  
14 IO::Buffered::Regexp - Regular expression 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($regexp, MaxSize => 0..Inf, Double => 0|1, ReturnsLast => 0|1)
31  
32 The C<IO::Buffered::Regexp> buffer type takes a regular expression as input and
33 splits records based on that. Only the match defined in the () is returned and
34 not the complete match.
35  
36 The options C<MaxSize> and C<Double> are optional.
37  
38 C<MaxSize> provides a limit on how big a buffer can grow, when the limit is hit
39 an exception is thrown.
40  
41 C<Double> defines the how the regexp is used to extract new items and remove
42 old data from the buffer. By default a while loop is used that grabs and
43 removes one item at a time from the buffer. If C<Double> is defined another
44 method is used:
45  
46 my @records = ($self->{buffer} =~ /$regexp/g);
47 $self->{buffer} =~ s/$regexp//g;
48  
49 This might be faster in some cases and slower in others.
50  
51 C<ReturnsLast> sets what the C<returns_last()> returns, this is used by the
52 "Split" buffer type to return the correct value as it is just a wrapper.
53  
54 =cut
55  
56 6 ▶sub new {
Subroutine new: called CC=11 SCAR=26.4
Pod documented
57 6 my ($class, $regexp, %opts) = @_;
58
59 # Check that $regexp is a Regexp or a non empty string
60 6 ▶ croak "Regexp should be a string or regexp" if !(defined $regexp and
Branch: 1 branch
BranchTrueFalseTotal
unless defined $regexp and ref $regexp eq "Regexp" || ref $regexp eq "" && $regexp ne "" 4 2 6
Condition77%7/9
defined $regexp and ref $regexp eq "Regexp" || ref $regexp eq "" && $regexp ne ""
!ll&&!rl&&r
132
ref $regexp eq "Regexp" or ref $regexp eq "" and $regexp ne ""
l!l&&r!l&&!r
203
ref $regexp eq "" and $regexp ne ""
!ll&&!rl&&r
210
MC/DC50%2/4
defined $regexp and ref $regexp eq "Regexp" || ref $regexp eq "" && $regexp ne ""
defined $regexp ref $regexp eq "Regexp" ref $regexp eq "" $regexp ne ""
Truth table80%4/5
A and B or C and D
A= defined $regexpB= ref $regexp eq "Regexp"C= ref $regexp eq ""D= $regexp ne ""
ABCDresult
0XXX0
100X0
10100
11XX1
10111
61 (ref $regexp eq 'Regexp' or (ref $regexp eq '' and $regexp ne '')));
62  
63 # Check that $regexp is a Regexp or a non empty string
64 croak "Option MaxSize should be a positiv integer" if $opts{MaxSize} and !(
65 2 ▶ $opts{MaxSize} =~ /^\d+$/ and $opts{MaxSize} > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"MaxSize"} and not $opts{"MaxSize"} =~ /^\d+$/ && $opts{"MaxSize"} > 0 0 2 2
Condition50%3/6
$opts{"MaxSize"} and not $opts{"MaxSize"} =~ /^\d+$/ && $opts{"MaxSize"} > 0
!ll&&!rl&&r
110
$opts{'MaxSize'} =~ /^\d+$/ && $opts{'MaxSize'} > 0
!ll&&!rl&&r
001
MC/DC0%0/3
$opts{"MaxSize"} and not $opts{"MaxSize"} =~ /^\d+$/ && $opts{"MaxSize"} > 0
$opts{"MaxSize"} $opts{'MaxSize'} =~ /^\d+$/ $opts{'MaxSize'} > 0
Truth table50%2/4
A and not(B && C)
A= $opts{"MaxSize"}B= $opts{'MaxSize'} =~ /^\d+$/C= $opts{'MaxSize'} > 0
ABCresult
0XX0
1110
10X1
1101
66
67 my %self = (
68 buffer => '',
69 regexp => qr/$regexp/,
70 maxsize => $opts{MaxSize},
71 double => ($opts{Double} or 0),
72 2 ▶ returns_last => (exists $opts{ReturnsLast} ? $opts{ReturnsLast} : 1),
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'ReturnsLast'} ? : 1 1 2
Condition50%1/2
$opts{'Double'} || 0
l!l
02
MC/DC0%0/1
$opts{'Double'} || 0
$opts{'Double'}
Truth table50%1/2
A
A= $opts{'Double'}
Aresult
11
00
73 );
74
75 2 ▶ return bless \%self, (ref $class || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
76 }
77  
78 =item flush($str, ...)
79  
80 =cut
81  
82 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
83 0 my $self = shift;
84 0 $self->{buffer} = join ('', @_);
85 }
86  
87 =item buffer()
88  
89 =cut
90  
91 0 ▶sub buffer {
Subroutine buffer: not called CC=1 SCAR=6.9
Pod documented
92 0 my $self = shift;
93 0 return $self->{buffer};
94 }
95  
96 =item write($str, ...)
97  
98 =cut
99  
100 4 ▶sub write {
Subroutine write: called CC=3 SCAR=11.2
Pod documented
101 4 my $self = shift;
102 4 my $str = join ('', @_);
103
104 4 ▶ if(my $maxsize = $self->{maxsize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $maxsize = $self->{"maxsize"}) 2 2 4
105 2 my $length = length($str) + length($self->{buffer});
106 2 ▶ if($length > $maxsize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > $maxsize) 0 2 2
107 0 croak "Buffer overrun";
108 }
109 }
110  
111 4 $self->{buffer} .= $str;
112 }
113  
114 =item read()
115  
116 =cut
117  
118 8 ▶sub read {
Subroutine read: called CC=4 SCAR=15.7
Pod documented
119 8 my ($self) = @_;
120  
121 8 ▶ if($self->{double}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'double'}) { } 0 8 8
122 0 my $regexp = $self->{regexp};
123 0 my @records = ($self->{buffer} =~ /$regexp/g);
124 0 $self->{buffer} =~ s/$regexp//g;
125 0 return @records;
126
127 } else {
128 8 my $regexp = $self->{regexp};
129 8 my @records;
130 8 ▶ while ($self->{buffer} =~ s/$regexp//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buffer"} =~ s/$regexp//) 6 8 14
131 6 ▶ if($1 ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 ne '') { } 6 0 6
132 6 push(@records, $1);
133 } else {
134 0 last;
135 }
136 }
137 8 return @records;
138 }
139 }
140  
141 =item returns_last()
142  
143 =cut
144  
145 0 ▶sub returns_last {
Subroutine returns_last: not called CC=1 SCAR=6.9
Pod documented
146 0 my $self = shift;
147 0 return $self->{returns_last};
148 }
149  
150 =item read_last()
151  
152 =cut
153  
154 4 ▶sub read_last {
Subroutine read_last: called CC=2 SCAR=6.9
Pod documented
155 4 my ($self) = @_;
156 4 my @results = $self->read();
157 4 ▶ push(@results, $self->{buffer}) if $self->{buffer} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"buffer"} ne "" 2 2 4
158 4 $self->{buffer} = '';
159 4 return @results;
160 }
161  
162 =back
163  
164 =head1 AUTHOR
165  
166 Troels Liebe Bentsen <tlb@rapanden.dk>
167  
168 =head1 COPYRIGHT
169  
170 Copyright(C) 2008 Troels Liebe Bentsen
171  
172 This library is free software; you can redistribute it and/or modify
173 it under the same terms as Perl itself.
174  
175 =cut
176  
177 9 1;
178  
« Buffered/HTTP.pm ↑ Summary Buffered/FixedSize.pm »