Message/Body/File.pm

filter: Statementstmt 57.5% 84 / 146 Branchbran 37.5% 18 / 48 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 18 / 24 Podpod 100% 10 / 10 totaltotal 56.2% 131 / 233 SCAR 46.0
CC
26
Cov
52%
_data_from_filename
20.0
nrLines
12.6
_data_from_glob
12.0
CRAP 99.4
« Message/Construct/Forward.pm ↑ Summary Message/Head/ResentGroup.pm »
1 # Copyrights 2001-2023 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::Message::Body::File;
10 34 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 $VERSION = '3.013';
12  
13 34 ▶use base 'Mail::Message::Body';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 34 ▶use Mail::Box::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 34 ▶use Mail::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 34 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 34 ▶use File::Temp qw/tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 34 ▶use File::Copy qw/copy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 sub _data_from_filename(@)
27 0 ▶{ my ($self, $filename) = @_;
Subroutine _data_from_filename: not called CC=4 SCAR=30.0
28  
29 0 local $_;
30 0 local (*IN, *OUT);
31  
32 0 ▶ unless(open IN, '<:raw', $filename)
Branch: 1 branch
BranchTrueFalseTotal
unless (open IN, "<:raw", $filename) 0 0 0
33 0 { $self->log(ERROR =>
34 "Unable to read file $filename for message body file: $!");
35 0 return;
36 }
37  
38 0 my $file = $self->tempFilename;
39 0 ▶ unless(open OUT, '>:raw', $file)
Branch: 1 branch
BranchTrueFalseTotal
unless (open OUT, ">:raw", $file) 0 0 0
40 0 { $self->log(ERROR => "Cannot write to temporary body file $file: $!");
41 0 return;
42 }
43  
44 0 my $nrlines = 0;
45 0 ▶ while(<IN>) { print OUT; $nrlines++ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 0 0 0
46  
47 0 close OUT;
48 0 close IN;
49  
50 0 $self->{MMBF_nrlines} = $nrlines;
51 0 $self;
52 }
53  
54 sub _data_from_filehandle(@)
55 1 ▶{ my ($self, $fh) = @_;
Subroutine _data_from_filehandle: called CC=3 SCAR=11.1
56 1 my $file = $self->tempFilename;
57 1 my $nrlines = 0;
58  
59 1 local *OUT;
60  
61 1 ▶ unless(open OUT, '>:raw', $file)
Branch: 1 branch
BranchTrueFalseTotal
unless (open OUT, ">:raw", $file) 0 1 1
62 0 { $self->log(ERROR => "Cannot write to temporary body file $file: $!");
63 0 return;
64 }
65  
66 1 ▶ while(my $l = $fh->getline)
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $fh->getline) 5 1 6
67 5 { print OUT $l;
68 5 $nrlines++;
69 }
70 1 close OUT;
71  
72 1 $self->{MMBF_nrlines} = $nrlines;
73 1 $self;
74 }
75  
76 sub _data_from_glob(@)
77 0 ▶{ my ($self, $fh) = @_;
Subroutine _data_from_glob: not called CC=3 SCAR=24.8
78 0 my $file = $self->tempFilename;
79 0 my $nrlines = 0;
80  
81 0 local $_;
82 0 local *OUT;
83  
84 0 ▶ unless(open OUT, '>:raw', $file)
Branch: 1 branch
BranchTrueFalseTotal
unless (open OUT, ">:raw", $file) 0 0 0
85 0 { $self->log(ERROR => "Cannot write to temporary body file $file: $!");
86 0 return;
87 }
88  
89 0 ▶ while(<$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
90 0 { print OUT;
91 0 $nrlines++;
92 }
93 0 close OUT;
94  
95 0 $self->{MMBF_nrlines} = $nrlines;
96 0 $self;
97 }
98  
99 sub _data_from_lines(@)
100 1 ▶{ my ($self, $lines) = @_;
Subroutine _data_from_lines: called CC=2 SCAR=7.2
101 1 my $file = $self->tempFilename;
102  
103 1 local *OUT;
104  
105 1 ▶ unless(open OUT, '>:raw', $file)
Branch: 1 branch
BranchTrueFalseTotal
unless (open OUT, ">:raw", $file) 0 1 1
106 0 { $self->log(ERROR => "Cannot write to $file: $!");
107 0 return;
108 }
109  
110 1 print OUT @$lines;
111 1 close OUT;
112  
113 1 $self->{MMBF_nrlines} = @$lines;
114 1 $self;
115 }
116  
117 sub clone()
118 0 ▶{ my $self = shift;
Subroutine clone: not called CC=2 SCAR=17.9
Pod documented
119 0 my $clone = ref($self)->new(based_on => $self);
120  
121 0 ▶ copy($self->tempFilename, $clone->tempFilename)
Branch: 1 branch
BranchTrueFalseTotal
copy($self->tempFilename, $clone->tempFilename) or return 0 0 0
122 or return;
123  
124 0 $clone->{MMBF_nrlines} = $self->{MMBF_nrlines};
125 0 $clone->{MMBF_size} = $self->{MMBF_size};
126 0 $self;
127 }
128  
129 sub nrLines()
130 2 ▶{ my $self = shift;
Subroutine nrLines: called CC=4 SCAR=25.3
Pod documented
131  
132 return $self->{MMBF_nrlines}
133 2 ▶ if defined $self->{MMBF_nrlines};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"MMBF_nrlines"} 2 0 2
134  
135 0 my $file = $self->tempFilename;
136 0 my $nrlines = 0;
137  
138 0 local $_;
139 0 local *IN;
140  
141 0 ▶ open IN, '<:raw', $file
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<:raw", $file or die "Cannot read from ${file}: $!\n" 0 0 0
142 or die "Cannot read from $file: $!\n";
143  
144 0 ▶ $nrlines++ while <IN>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline IN) and do { ++$nrlines } 0 0 0
145 0 close IN;
146  
147 0 $self->{MMBF_nrlines} = $nrlines;
148 }
149  
150 #------------------------------------------
151  
152 sub size()
153 2 ▶{ my $self = shift;
Subroutine size: called CC=3 SCAR=11.2
Pod documented
154  
155 return $self->{MMBF_size}
156 2 ▶ if exists $self->{MMBF_size};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"MMBF_size"} 0 2 2
157  
158 2 my $size = eval { -s $self->tempFilename };
159  
160 2 ▶ $size -= $self->nrLines
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Message::crlf_platform 0 2 2
161 if $Mail::Message::crlf_platform; # remove count for extra CR's
162  
163 2 $self->{MMBF_size} = $size;
164 }
165  
166 sub string()
167 3 ▶{ my $self = shift;
Subroutine string: called CC=2 SCAR=7.0
Pod documented
168  
169 3 my $file = $self->tempFilename;
170  
171 3 local *IN;
172  
173 3 ▶ open IN, '<:raw', $file
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<:raw", $file or die "Cannot read from ${file}: $!\n" 0 3 3
174 or die "Cannot read from $file: $!\n";
175  
176 3 my $return = join '', <IN>;
177 3 close IN;
178  
179 3 $return;
180 }
181  
182 sub lines()
183 3 ▶{ my $self = shift;
Subroutine lines: called CC=3 SCAR=11.0
Pod documented
184  
185 3 my $file = $self->tempFilename;
186  
187 3 local *IN;
188 3 ▶ open IN, '<:raw', $file
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<:raw", $file or die "Cannot read from ${file}: $!\n" 0 3 3
189 or die "Cannot read from $file: $!\n";
190  
191 3 my @r = <IN>;
192 3 close IN;
193  
194 3 $self->{MMBF_nrlines} = @r;
195 3 ▶ wantarray ? @r: \@r;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
196 }
197  
198 sub file()
199 0 ▶{ open my $tmp, '<:raw', shift->tempFilename;
Subroutine file: not called CC=1 SCAR=6.9
Pod documented
200 0 $tmp;
201 }
202  
203 sub print(;$)
204 2 ▶{ my $self = shift;
Subroutine print: called CC=6 SCAR=19.7
Pod documented
205 2 ▶ my $fh = shift || select;
Condition33%1/3
shift() || select()
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || select()
shift() select()
Truth table33%1/3
A || B
A= shift()B= select()
ABresult
1X1
011
000
206 2 my $file = $self->tempFilename;
207  
208 2 local $_;
209 2 local *IN;
210  
211 2 ▶ open IN, '<:raw', $file
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<:raw", $file or croak("Cannot read from ${file}: $!\n") 0 2 2
212 or croak "Cannot read from $file: $!\n";
213  
214 2 ▶ if(ref $fh eq 'GLOB') {print $fh $_ while <IN>}
Branch: 2 branches
BranchTrueFalseTotal
if (ref $fh eq 'GLOB') { } 0 2 2
defined($_ = readline IN) and do { print $fh $_ } 0 0 0
215 2 ▶ else {$fh->print($_) while <IN>}
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline IN) and do { $fh->print($_) } 10 2 12
216 2 close IN;
217  
218 2 $self;
219 }
220  
221 sub read($$;$@)
222 0 ▶{ my ($self, $parser, $head, $bodytype) = splice @_, 0, 4;
Subroutine read: not called CC=2 SCAR=17.9
Pod documented
223 0 my $file = $self->tempFilename;
224  
225 0 local *OUT;
226  
227 0 ▶ open OUT, '>:raw', $file
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">:raw", $file or die "Cannot write to ${file}: $!.\n" 0 0 0
228 or die "Cannot write to $file: $!.\n";
229  
230 0 (my $begin, my $end, $self->{MMBF_nrlines}) = $parser->bodyAsFile(\*OUT,@_);
231 0 close OUT;
232  
233 0 $self->fileLocation($begin, $end);
234 0 $self;
235 }
236  
237 # on UNIX always true. Expensive to calculate on Windows: message size
238 # may be off-by-one in rare cases.
239 0 ▶sub endsOnNewline() { shift->size==0 }
Subroutine endsOnNewline: not called CC=1 SCAR=6.9
Pod documented
240  
241 #------------------------------------------
242  
243  
244 sub tempFilename(;$)
245 15 ▶{ my $self = shift;
Subroutine tempFilename: called CC=3 SCAR=11.1
Pod documented
246  
247 @_ ? ($self->{MMBF_filename} = shift)
248 : $self->{MMBF_filename} ? $self->{MMBF_filename}
249 15 ▶ : ($self->{MMBF_filename} = (tempfile)[1]);
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 15 15
$self->{'MMBF_filename'} ? : 13 2 15
250 }
251  
252 #------------------------------------------
253  
254  
255 2 ▶sub DESTROY { unlink shift->tempFilename }
Subroutine DESTROY: called CC=1 SCAR=0.0
256  
257 #------------------------------------------
258  
259 34 1;
« Message/Construct/Forward.pm ↑ Summary Message/Head/ResentGroup.pm »