blib/lib/Email/Simple/FromHandle.pm

filter: Statementstmt 92.5% 75 / 81 Branchbran 66.6% 16 / 24 Conditioncond 63.6% 14 / 22 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 94.4% 17 / 18 Podpod 100% 8 / 8 totaltotal 81.9% 136 / 166 SCAR 32.0
CC
22
Cov
83%
getline
12.0
stream_to
7.1
_split_head_from_body
5.0
CRAP 24.5
↑ Summary
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::Simple::FromHandle;
4 {
5 5 $Email::Simple::FromHandle::VERSION = '0.054';
6 }
7 5 ▶use Email::Simple 2.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use parent 'Email::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # ABSTRACT: an Email::Simple but from a handle
10  
11  
12 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 my $crlf = qr/\x0a\x0d|\x0d\x0a|\x0a|\x0d/; # We are liberal in what we accept.
17  
18  
19 45 ▶sub handle { $_[0]->{handle} }
Subroutine handle: called CC=1 SCAR=0.0
Pod documented
20  
21  
22 31 ▶sub body_pos { $_[0]->{body_pos} }
Subroutine body_pos: called CC=1 SCAR=0.0
Pod documented
23  
24  
25 sub _is_seekable {
26 16 ▶ my ($self) = @_;
Subroutine _is_seekable: called CC=3 SCAR=11.0
27 # on solaris, tell($pipe) == -1, and seeking on a pipe appears to discard the
28 # data waiting
29 16 ▶ return unless $self->body_pos >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->body_pos >= 0 0 16 16
30 # on linux, seeking on a pipe is safe and returns ''
31 16 ▶ return unless seek($self->handle, 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless seek $self->handle, 0, 1 2 14 16
32 # fall through: it must be seekable
33 14 return 1;
34 }
35  
36 sub reset_handle {
37 16 ▶ my ($self) = @_;
Subroutine reset_handle: called CC=4 SCAR=13.9
Pod documented
38  
39 # Don't die the first time we try to read from a pipe/socket/etc.
40 # TODO: When reading from something non-seekable, should we
41 # give the option to store data into a temp file, or something similar?
42 16 ▶ return unless $self->_is_seekable || $self->{_seek}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_seekable or $self->{"_seek"}++ 1 15 16
Condition100%3/3
$self->_is_seekable or $self->{"_seek"}++
l!l&&r!l&&!r
1411
MC/DC100%2/2
$self->_is_seekable or $self->{"_seek"}++
$self->_is_seekable $self->{"_seek"}++
Truth table100%3/3
A or B
A= $self->_is_seekableB= $self->{"_seek"}++
ABresult
1X1
011
000
43  
44 15 delete $self->{_get_head_lines};
45  
46 15 ▶ seek $self->handle, $self->body_pos, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
seek $self->handle, $self->body_pos, 0 or Carp::croak("can't seek: $!") 1 14 15
47 or Carp::croak "can't seek: $!";
48 }
49  
50  
51 sub getline {
52 0 ▶ my ($self) = @_;
Subroutine getline: not called CC=3 SCAR=24.8
Pod documented
53 0 ▶ unless ($self->{_get_head_lines}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_get_head_lines"}) 0 0 0
54 $self->{_get_head_lines} = [
55 0 split(/(?<=\n)/, $self->header_obj->as_string),
56 $self->crlf,
57 ];
58 }
59 0 my $handle = $self->handle;
60 0 ▶ return shift @{$self->{_get_head_lines}} || <$handle>;
Condition0%0/3
shift @{$$self{'_get_head_lines'};} || readline $handle
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @{$$self{'_get_head_lines'};} || readline $handle
shift @{$$self{'_get_head_lines'};} readline $handle
Truth table0%0/3
A || B
A= shift @{$$self{'_get_head_lines'};}B= readline $handle
ABresult
1X1
011
000
61 }
62  
63  
64 sub _stream_to_print {
65 2 ▶ my $fh = shift;
Subroutine _stream_to_print: called CC=2 SCAR=7.1
66 2 ▶ print {$fh} @_ or Carp::croak "can't print buffer: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} @_ or Carp::croak("can't print buffer: $!") 0 2 2
67 }
68  
69 sub stream_to {
70 3 ▶ my ($self, $fh, $arg) = @_;
Subroutine stream_to: called CC=7 SCAR=19.6
Pod documented
71 3 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
21
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
72 3 ▶ $arg->{reset_handle} = 1 unless exists $arg->{reset_handle};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg->{"reset_handle"} 3 0 3
73 # 65536 is a randomly-chosen magical number that's large enough to be a win
74 # over line-by-line reading but small enough not to impinge very much upon
75 # ram usage -- hdp, 2006-11-27
76 3 ▶ $arg->{chunk_size} ||= 65536;
Condition50%1/2
$arg->{"chunk_size"} ||= 65536
l!l
03
MC/DC0%0/1
$arg->{"chunk_size"} ||= 65536
$arg->{"chunk_size"}
Truth table50%1/2
A
A= $arg->{"chunk_size"}
Aresult
11
00
77 3 ▶ $arg->{write} ||= \&_stream_to_print;
Condition100%2/2
$arg->{"write"} ||= \&Email::Simple::FromHandle::_stream_to_print
l!l
21
MC/DC100%1/1
$arg->{"write"} ||= \&Email::Simple::FromHandle::_stream_to_print
$arg->{"write"}
Truth table100%2/2
A
A= $arg->{"write"}
Aresult
11
00
78 3 $arg->{write}->($fh, $self->header_obj->as_string . $self->crlf);
79 3 ▶ $self->reset_handle if $arg->{reset_handle};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"reset_handle"} 3 0 3
80 3 my $buf;
81 3 ▶ while (read($self->handle, $buf, $arg->{chunk_size}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (read($self->handle, $buf, $arg->{"chunk_size"}) > 0) 3 3 6
82 3 $arg->{write}->($fh, $buf);
83 }
84 }
85  
86 #### Methods that override Email::Simple below
87  
88 sub new {
89 6 ▶ my ($class, $handle, $arg) = @_;
Subroutine new: called CC=4 SCAR=14.6
Pod documented
90  
91 6 ▶ $arg ||= {};
Condition50%1/2
$arg ||= {}
l!l
06
MC/DC0%0/1
$arg ||= {}
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
92 6 ▶ $arg->{header_class} ||= $class->default_header_class;
Condition33%1/3
$arg->{"header_class"} ||= $class->default_header_class
l!l&&r!l&&!r
060
MC/DC0%0/2
$arg->{"header_class"} ||= $class->default_header_class
$arg->{"header_class"} $class->default_header_class
Truth table33%1/3
A ||= B
A= $arg->{"header_class"}B= $class->default_header_class
ABresult
1X1
011
000
93  
94 6 ▶ return Email::Simple->new($handle, $arg) unless ref $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $handle 0 6 6
95  
96 6 my ($head, $mycrlf) = $class->_split_head_from_body($handle);
97  
98 6 my $self = bless {
99 handle => $handle,
100 body_pos => tell($handle),
101 mycrlf => $mycrlf,
102 }, $class;
103  
104 $self->header_obj_set(
105 6 $arg->{header_class}->new($head, { crlf => $self->crlf })
106 );
107  
108 6 return $self;
109 }
110  
111 sub _split_head_from_body {
112 6 ▶ my ($class, $handle) = @_;
Subroutine _split_head_from_body: called CC=5 SCAR=16.2
113  
114 6 my $text = q{};
115  
116 # XXX it is stupid to use <> if we're really going to have multiple forms
117 # of crlf, but it is expedient to keep doing so for now. -- hdp, 2006-11-28
118 # theoretically, this should be ok, because it will only fail if lines are
119 # terminated with \x0d, which wouldn't be ok for network transport anyway.
120 6 my $mycrlf;
121 6 ▶ while (<$handle>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $handle)) 123 0 123
122 123 ▶ last if $mycrlf and /\A$mycrlf\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $mycrlf and /\A$mycrlf\z/ 6 117 123
Condition100%3/3
$mycrlf and /\A$mycrlf\z/
!ll&&!rl&&r
61116
MC/DC100%2/2
$mycrlf and /\A$mycrlf\z/
$mycrlf /\A$mycrlf\z/
Truth table100%3/3
A and B
A= $mycrlfB= /\A$mycrlf\z/
ABresult
0X0
100
111
123 117 $text .= $_;
124 117 ($mycrlf) = /($crlf)\z/;
125 }
126  
127 6 ▶ return ($text, $mycrlf || "\n");
Condition50%1/2
$mycrlf || "\n"
l!l
60
MC/DC0%0/1
$mycrlf || "\n"
$mycrlf
Truth table50%1/2
A
A= $mycrlf
Aresult
11
00
128 }
129  
130 sub body_set {
131 2 ▶ my $self = shift;
Subroutine body_set: called CC=1 SCAR=0.0
Pod documented
132 2 my $body = shift;
133  
134 2 my $handle = IO::String->new(\$body);
135 2 $self->{handle} = $handle;
136 2 $self->{body_pos} = 0;
137 }
138  
139 sub body {
140 7 ▶ my $self = shift;
Subroutine body: called CC=1 SCAR=0.0
Pod documented
141 7 scalar do {
142 7 local $/; ## no critic Local, Punctuation
143 7 $self->reset_handle;
144 6 my $handle = $self->handle;
145 6 <$handle>;
146 };
147 }
148  
149  
150 5 1;
151  
152 __END__
↑ Summary