Box/Parser/Perl.pm

filter: Statementstmt 64.3% 110 / 171 Branchbran 43.7% 35 / 80 Conditioncond 24.2% 8 / 33 MC/DCmcdc 9.5% 2 / 21 Subroutinesub 73.9% 17 / 23 Podpod 92.3% 12 / 13 totaltotal 53.9% 184 / 341 CC 55 SCAR 28.9
CC
55
Cov
54%
_is_good_end
72.0
bodyAsString
30.0
_read_stripped_lines
19.8
CRAP 17.9
« Message/Head/Partial.pm ↑ Summary Message/Body/Construct.pm »
1 # Copyrights 2001-2022 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of distribution Mail-Message. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Box::Parser::Perl;
10 5 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 $VERSION = '3.012';
12  
13 5 ▶use base 'Mail::Box::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use Mail::Message::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use List::Util 'sum';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 sub init(@)
24 7 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
25  
26 7 ▶ $self->SUPER::init($args) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init($args) or return 0 7 7
27  
28 7 $self->{MBPP_trusted} = $args->{trusted};
29 7 $self->{MBPP_fix} = $args->{fix_header_errors};
30 7 $self;
31 }
32  
33 sub pushSeparator($)
34 2 ▶{ my ($self, $sep) = @_;
Subroutine pushSeparator: called CC=2 SCAR=7.0
Pod documented
35 2 unshift @{$self->{MBPP_separators}}, $sep;
36 2 ▶ $self->{MBPP_strip_gt}++ if $sep eq 'From ';
Branch: 1 branch
BranchTrueFalseTotal
if $sep eq "From " 0 2 2
37 2 $self;
38 }
39  
40 sub popSeparator()
41 2 ▶{ my $self = shift;
Subroutine popSeparator: called CC=2 SCAR=7.0
Pod documented
42 2 my $sep = shift @{$self->{MBPP_separators}};
43 2 ▶ $self->{MBPP_strip_gt}-- if $sep eq 'From ';
Branch: 1 branch
BranchTrueFalseTotal
if $sep eq "From " 0 2 2
44 2 $sep;
45 }
46
47 sub filePosition(;$)
48 2 ▶{ my $self = shift;
Subroutine filePosition: called CC=2 SCAR=7.2
Pod documented
49 2 ▶ @_ ? $self->{MBPP_file}->seek(shift, 0) : $self->{MBPP_file}->tell;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 2 2
50 }
51  
52 5 my $empty = qr/^\015?\012?$/;
53  
54  
55 sub readHeader()
56 12 ▶{ my $self = shift;
Subroutine readHeader: called CC=9 SCAR=26.8
Pod documented
57 12 my $file = $self->{MBPP_file};
58  
59 12 my @ret = ($file->tell, undef);
60 12 my $line = $file->getline;
61  
62 LINE:
63 12 ▶ while(defined $line)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 46 0 46
64 46 ▶ { last LINE if $line =~ $empty;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$empty/ 12 34 46
65 34 my ($name, $body) = split /\s*\:\s*/, $line, 2;
66  
67 34 ▶ unless(defined $body)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 34 34
68 0 { $self->log(WARNING =>
69 "Unexpected end of header in ".$self->filename.":\n $line");
70  
71 0 ▶ if(@ret && $self->fixHeaderErrors)
Branch: 1 branch
BranchTrueFalseTotal
if (@ret and $self->fixHeaderErrors) { } 0 0 0
Condition0%0/3
@ret and $self->fixHeaderErrors
!ll&&!rl&&r
000
MC/DC0%0/2
@ret and $self->fixHeaderErrors
@ret $self->fixHeaderErrors
Truth table0%0/3
A and B
A= @retB= $self->fixHeaderErrors
ABresult
0X0
100
111
72 0 { $ret[-1][1] .= ' '.$line; # glue err line to previous field
73 0 $line = $file->getline;
74 0 next LINE;
75 }
76 else
77 0 { $file->seek(-length $line, 1);
78 0 last LINE;
79 }
80 }
81  
82 34 ▶ $body = "\n" unless length $body;
Branch: 1 branch
BranchTrueFalseTotal
unless length $body 0 34 34
83  
84 # Collect folded lines
85 34 ▶ while($line = $file->getline)
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $file->getline) 37 0 37
86 37 ▶ { $line =~ m!^[ \t]! ? ($body .= $line) : last;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^[ \t]/ ? : 3 34 37
87 }
88  
89 34 $body =~ s/\015//g;
90 34 push @ret, [ $name, $body ];
91 }
92  
93 12 $ret[1] = $file->tell;
94 12 @ret;
95 }
96  
97 sub _is_good_end($)
98 0 ▶{ my ($self, $where) = @_;
Subroutine _is_good_end: not called CC=8 SCAR=42.8
99  
100 # No seps, then when have to trust it.
101 0 my $sep = $self->{MBPP_separators}[0];
102 0 ▶ return 1 unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 0 0 0
103  
104 0 my $file = $self->{MBPP_file};
105 0 my $here = $file->tell;
106 0 ▶ $file->seek($where, 0) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$file->seek($where, 0) or return 0 0 0 0
107  
108 # Find first non-empty line on specified location.
109 0 my $line = $file->getline;
110 0 ▶ $line = $file->getline while defined $line && $line =~ $empty;
Branch: 1 branch
BranchTrueFalseTotal
defined $line and $line =~ /$empty/ and do { $line = $file->getline } 0 0 0
Condition0%0/3
defined $line and $line =~ /$empty/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $line and $line =~ /$empty/
defined $line $line =~ /$empty/
Truth table0%0/3
A and B
A= defined $lineB= $line =~ /$empty/
ABresult
0X0
100
111
111  
112 # Check completed, return to old spot.
113 0 $file->seek($here, 0);
114 0 ▶ return 1 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
115  
116 0 ▶ substr($line, 0, length $sep) eq $sep
Branch: 1 branch
BranchTrueFalseTotal
substr($line, 0, length $sep) eq $sep and $sep ne "From " || $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/ 0 0 0
Condition0%0/6
substr($line, 0, length $sep) eq $sep && ($sep ne 'From ' || $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/)
!ll&&!rl&&r
000
$sep ne "From " or $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/
l!l&&r!l&&!r
000
MC/DC0%0/3
substr($line, 0, length $sep) eq $sep && ($sep ne 'From ' || $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/)
substr($line, 0, length $sep) eq $sep $sep ne "From " $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/
Truth table0%0/4
A && B or C
A= substr($line, 0, length $sep) eq $sepB= $sep ne "From "C= $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/
ABCresult
0XX0
1000
11X1
1011
117 && ($sep ne 'From ' || $line =~ m/ (?:19[6-9]|20[0-3])[0-9]\b/ );
118 }
119  
120 sub readSeparator()
121 6 ▶{ my $self = shift;
Subroutine readSeparator: called CC=6 SCAR=21.2
Pod documented
122  
123 6 my $sep = $self->{MBPP_separators}[0];
124 6 ▶ return () unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 0 6 6
125  
126 6 my $file = $self->{MBPP_file};
127 6 my $start = $file->tell;
128  
129 6 my $line = $file->getline;
130 6 ▶ while(defined $line && $line =~ $empty)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and $line =~ /$empty/) 0 6 6
Condition33%1/3
defined $line and $line =~ /$empty/
!ll&&!rl&&r
060
MC/DC0%0/2
defined $line and $line =~ /$empty/
defined $line $line =~ /$empty/
Truth table33%1/3
A and B
A= defined $lineB= $line =~ /$empty/
ABresult
0X0
100
111
131 0 { $start = $file->tell;
132 0 $line = $file->getline;
133 }
134  
135 6 ▶ return () unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 6 6
136  
137 6 $line =~ s/[\012\015]+$/\n/;
138 6 ▶ return ($start, $line)
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, length $sep) eq $sep 6 0 6
139 if substr($line, 0, length $sep) eq $sep;
140  
141 0 $file->seek($start, 0);
142 0 ();
143 }
144  
145 sub _read_stripped_lines(;$$)
146 13 ▶{ my ($self, $exp_chars, $exp_lines) = @_;
Subroutine _read_stripped_lines: called CC=17 SCAR=29.9
147 13 my @seps = @{$self->{MBPP_separators}};
148  
149 13 my $file = $self->{MBPP_file};
150 13 my $lines = [];
151 13 my $msgend;
152  
153 13 ▶ if(@seps)
Branch: 1 branch
BranchTrueFalseTotal
if (@seps) { } 6 7 13
154 {
155 LINE:
156 6 while(1)
157 24 { my $where = $file->getpos;
158 24 ▶ my $line = $file->getline
Branch: 1 branch
BranchTrueFalseTotal
my $line = $file->getline or last LINE 0 24 24
159 or last LINE;
160  
161 24 foreach my $sep (@seps)
162 39 ▶ { next if substr($line, 0, length $sep) ne $sep;
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, length $sep) ne $sep 33 6 39
163  
164 # Some apps fail to escape lines starting with From
165 6 ▶ next if $sep eq 'From ' && $line !~ m/ 19[789][0-9]| 20[0-9][0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $sep eq "From " and not $line =~ / 19[789][0-9]| 20[0-9][0-9]/ 0 6 6
Condition33%1/3
$sep eq "From " and not $line =~ / 19[789][0-9]| 20[0-9][0-9]/
!ll&&!rl&&r
600
MC/DC0%0/2
$sep eq "From " and not $line =~ / 19[789][0-9]| 20[0-9][0-9]/
$sep eq "From " not $line =~ / 19[789][0-9]| 20[0-9][0-9]/
Truth table33%1/3
A and not(B)
A= $sep eq "From "B= not $line =~ / 19[789][0-9]| 20[0-9][0-9]/
ABresult
0X0
100
111
166  
167 6 $file->setpos($where);
168 6 $msgend = $file->tell;
169 6 last LINE;
170 }
171  
172 18 push @$lines, $line;
173 }
174  
175 6 ▶ if(@$lines && $lines->[-1] =~ s/(\r?\n)\z//)
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines and $lines->[-1] =~ s/(\r?\n)\z//) 5 1 6
Condition66%2/3
@$lines and $lines->[-1] =~ s/(\r?\n)\z//
!ll&&!rl&&r
105
MC/DC50%1/2
@$lines and $lines->[-1] =~ s/(\r?\n)\z//
@$lines $lines->[-1] =~ s/(\r?\n)\z//
Truth table66%2/3
A and B
A= @$linesB= $lines->[-1] =~ s/(\r?\n)\z//
ABresult
0X0
100
111
176 5 ▶ { pop @$lines if @seps==1 && length($lines->[-1])==0;
Branch: 1 branch
BranchTrueFalseTotal
if @seps == 1 and length $lines->[-1] == 0 2 3 5
Condition66%2/3
@seps == 1 and length $lines->[-1] == 0
!ll&&!rl&&r
302
MC/DC50%1/2
@seps == 1 and length $lines->[-1] == 0
@seps == 1 length $lines->[-1] == 0
Truth table66%2/3
A and B
A= @seps == 1B= length $lines->[-1] == 0
ABresult
0X0
100
111
177 }
178 }
179 else # File without separators.
180 7 ▶ { $lines = ref $file eq 'Mail::Box::FastScalar'
Branch: 1 branch
BranchTrueFalseTotal
ref $file eq 'Mail::Box::FastScalar' ? : 7 0 7
181 ? $file->getlines : [ $file->getlines ];
182 }
183  
184 13 my $bodyend = $file->tell;
185 13 ▶ if($lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($lines) 13 0 13
186 13 ▶ { if($self->{MBPP_strip_gt})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MBPP_strip_gt"}) 0 13 13
187 0 { s/^\>(\>*From\s)/$1/ for @$lines;
188 }
189 13 ▶ unless($self->{MBPP_trusted})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"MBPP_trusted"}) 0 13 13
190 0 { s/\015$// for @$lines;
191 # input is read as binary stream (i.e. preserving CRLF on Windows).
192 # Code is based on this assumption. Removal of CR if not trusted
193 # conflicts with this assumption. [Markus Spann]
194 }
195 }
196 #warn "($bodyend, $msgend, ".@$lines, ")\n";
197  
198 13 ($bodyend, $lines, $msgend);
199 }
200  
201 sub _take_scalar($$)
202 0 ▶{ my ($self, $begin, $end) = @_;
Subroutine _take_scalar: not called CC=1 SCAR=6.9
203 0 my $file = $self->{MBPP_file};
204 0 $file->seek($begin, 0);
205  
206 0 my $return;
207 0 $file->read($return, $end-$begin);
208 0 $return =~ s/\015//g;
209 0 $return;
210 }
211  
212 sub bodyAsString(;$$)
213 0 ▶{ my ($self, $exp_chars, $exp_lines) = @_;
Subroutine bodyAsString: not called CC=5 SCAR=34.0
Pod documented
214 0 my $file = $self->{MBPP_file};
215 0 my $begin = $file->tell;
216  
217 0 ▶ if(defined $exp_chars && $exp_chars>=0)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exp_chars and $exp_chars >= 0) 0 0 0
Condition0%0/3
defined $exp_chars and $exp_chars >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $exp_chars and $exp_chars >= 0
defined $exp_chars $exp_chars >= 0
Truth table0%0/3
A and B
A= defined $exp_charsB= $exp_chars >= 0
ABresult
0X0
100
111
218 { # Get at once may be successful
219 0 my $end = $begin + $exp_chars;
220  
221 0 ▶ if($self->_is_good_end($end))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_good_end($end)) 0 0 0
222 0 { my $body = $self->_take_scalar($begin, $end);
223 0 ▶ $body =~ s/^\>(\>*From\s)/$1/gm if $self->{MBPP_strip_gt};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"MBPP_strip_gt"} 0 0 0
224 0 return ($begin, $file->tell, $body);
225 }
226 }
227  
228 0 my ($end, $lines) = $self->_read_stripped_lines($exp_chars, $exp_lines);
229 0 return ($begin, $end, join('', @$lines));
230 }
231  
232 sub bodyAsList(;$$)
233 13 ▶{ my ($self, $exp_chars, $exp_lines) = @_;
Subroutine bodyAsList: called CC=1 SCAR=0.0
Pod documented
234 13 my $file = $self->{MBPP_file};
235 13 my $begin = $file->tell;
236  
237 13 my ($end, $lines) = $self->_read_stripped_lines($exp_chars, $exp_lines);
238 13 ($begin, $end, $lines);
239 }
240  
241 sub bodyAsFile($;$$)
242 0 ▶{ my ($self, $out, $exp_chars, $exp_lines) = @_;
Subroutine bodyAsFile: not called CC=2 SCAR=17.9
Pod documented
243 0 my $file = $self->{MBPP_file};
244 0 my $begin = $file->tell;
245  
246 0 my ($end, $lines) = $self->_read_stripped_lines($exp_chars, $exp_lines);
247  
248 0 $out->print($_) for @$lines;
249 0 ($begin, $end, scalar @$lines);
250 }
251  
252 sub bodyDelayed(;$$)
253 0 ▶{ my ($self, $exp_chars, $exp_lines) = @_;
Subroutine bodyDelayed: not called CC=3 SCAR=24.8
Pod documented
254 0 my $file = $self->{MBPP_file};
255 0 my $begin = $file->tell;
256  
257 0 ▶ if(defined $exp_chars)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exp_chars) 0 0 0
258 0 { my $end = $begin + $exp_chars;
259  
260 0 ▶ if($self->_is_good_end($end))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_good_end($end)) 0 0 0
261 0 { $file->seek($end, 0);
262 0 return ($begin, $end, $exp_chars, $exp_lines);
263 }
264 }
265  
266 0 my ($end, $lines) = $self->_read_stripped_lines($exp_chars, $exp_lines);
267 0 my $chars = sum(map length, @$lines);
268 0 ($begin, $end, $chars, scalar @$lines);
269 }
270  
271 sub openFile($)
272 7 ▶{ my ($self, $args) = @_;
Subroutine openFile: called CC=6 SCAR=20.2
Pod documented
273 7 ▶ my $mode = $args->{mode} or die "mode required";
Branch: 1 branch
BranchTrueFalseTotal
my $mode = $args->{"mode"} or die "mode required" 0 7 7
274 7 ▶ my $fh = $args->{file} || IO::File->new($args->{filename}, $mode);
Condition33%1/3
$args->{'file'} || 'IO::File'->new($args->{'filename'}, $mode)
l!l&&r!l&&!r
700
MC/DC0%0/2
$args->{'file'} || 'IO::File'->new($args->{'filename'}, $mode)
$args->{'file'} 'IO::File'->new($args->{'filename'}, $mode)
Truth table33%1/3
A || B
A= $args->{'file'}B= 'IO::File'->new($args->{'filename'}, $mode)
ABresult
1X1
011
000
275  
276 7 ▶ return unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 7 7
277 7 $self->{MBPP_file} = $fh;
278  
279 7 ▶ $fh->binmode(':raw')
Branch: 1 branch
BranchTrueFalseTotal
if $fh->can("binmode") or $fh->can("BINMODE") 7 0 7
Condition33%1/3
$fh->can("binmode") or $fh->can("BINMODE")
l!l&&r!l&&!r
700
MC/DC0%0/2
$fh->can("binmode") or $fh->can("BINMODE")
$fh->can("binmode") $fh->can("BINMODE")
Truth table33%1/3
A or B
A= $fh->can("binmode")B= $fh->can("BINMODE")
ABresult
1X1
011
000
280 if $fh->can('binmode') || $fh->can('BINMODE');
281  
282 7 $self->{MBPP_separators} = [];
283  
284 # binmode $fh, ':crlf' if $] < 5.007; # problem with perlIO
285 7 $self;
286 }
287  
288 sub closeFile()
289 14 ▶{ my $self = shift;
Subroutine closeFile: called CC=2 SCAR=6.9
Pod documented
290  
291 14 delete $self->{MBPP_separators};
292 14 delete $self->{MBPP_strip_gt};
293  
294 14 ▶ my $file = delete $self->{MBPP_file} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = delete $self->{"MBPP_file"} or return 7 7 14
295 7 $file->close;
296 7 $self;
297 }
298  
299 #------------------------------------------
300  
301  
302 sub fixHeaderErrors(;$)
303 0 ▶{ my $self = shift;
Subroutine fixHeaderErrors: not called CC=2 SCAR=17.9
Pod documented
304 0 ▶ @_ ? ($self->{MBPP_fix} = shift) : $self->{MBPP_fix};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
305 }
306  
307 #------------------------------------------
308  
309 5 1;
« Message/Head/Partial.pm ↑ Summary Message/Body/Construct.pm »