Mail/Make/Stream.pm

filter: Statementstmt 70.4% 105 / 149 Branchbran 42.4% 28 / 66 Conditioncond 33.3% 8 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 76.6% 23 / 30 Podpod 33.3% 1 / 3 totaltotal 57.6% 165 / 286 CC 43 SCAR 41.1
CC
43
Cov
59%
_get_glob_from_arg
141.1
__ANON__
42.0
__ANON__
42.0
CRAP 60.8
« Mail/Make/Entity.pm ↑ Summary Mail/Make/Stream/QuotedPrint.pm »
1 ##----------------------------------------------------------------------------
2 ## MIME Email Builder - ~/lib/Mail/Make/Stream.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/03/05
7 ## Modified 2026/03/05
8 ## All rights reserved.
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Mail::Make::Stream;
14 BEGIN
15 8 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 warnings::register_categories( 'Mail::Make' );
19 8 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶ use vars qw( $VERSION $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶ use Mail::Make::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
23 8 our $VERSION = 'v0.1.0';
24 };
25  
26 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 sub init
30 0 ▶{
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
31 0 my $self = shift( @_ );
32 0 $self->{_exception_class} = $EXCEPTION_CLASS;
33 0 $self->{_init_strict_use_sub} = 1;
34 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
35 0 return( $self );
36 }
37  
38 # NOTE: STORABLE support
39  
40 # NOTE: FREEZE is inherited
41  
42 0 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
43  
44 0 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
45  
46 # NOTE: THAW is inherited
47  
48 8 1;
49  
50 # NOTE: Mail::Make::Stream::Generic inline class
51 {
52 package
53 Mail::Make::Stream::Generic;
54 BEGIN
55 8 ▶ {
Subroutine BEGIN: called CC=1 SCAR=0.0
56 8 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 8 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 8 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
59 8 ▶ use vars qw( $VERSION $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
60 8 ▶ use Mail::Make::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 8 ▶ use Module::Generic::File::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 8 ▶ use Module::Generic::Scalar::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 8 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
64 8 our $VERSION = $Mail::Make::Stream::VERSION;
65 };
66  
67 8 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 8 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 sub init
71 64 ▶ {
Subroutine init: called CC=4 SCAR=14.3
72 64 my $self = shift( @_ );
73 64 ▶ my $class = ( ref( $self ) || $self );
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
6400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
74 64 $self->{_init_strict_use_sub} = 1;
75 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 64 $self->{_exception_class} = defined( ${"${class}::EXCEPTION_CLASS"} )
77 64 ▶ ? ${"${class}::EXCEPTION_CLASS"}
Branch: 1 branch
BranchTrueFalseTotal
defined ${"${class}::EXCEPTION_CLASS";} ? : 64 0 64
78 : $EXCEPTION_CLASS;
79 64 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 64 0 64
80 64 return( $self );
81 }
82  
83 # _get_glob_from_arg( $source_or_dest [, write => 1] )
84 #
85 # Normalises any of the following into a ( $fh, $op ) pair:
86 #
87 # - A native Perl glob / IO object → used directly
88 # - A scalar reference (\$scalar) → Module::Generic::Scalar::IO
89 # - A plain string (file path) → Module::Generic::File opened via new_file()
90 #
91 # $op is a closure that:
92 # Read mode: $op->( $buf, $len ) - returns bytes read (0 at EOF, undef on error)
93 # Write mode: $op->( $data ) - returns true on success, undef on error
94 sub _get_glob_from_arg
95 142 ▶ {
Subroutine _get_glob_from_arg: called CC=27 SCAR=49.5
96 142 my $self = shift( @_ );
97 142 my $this = shift( @_ );
98 142 ▶ if( !defined( $this ) || ( !ref( $this ) && !length( $this ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this and ref $this || length $this) 0 142 142
Condition33%2/6
defined $this and ref $this || length $this
!ll&&!rl&&r
00142
ref $this or length $this
l!l&&r!l&&!r
14200
MC/DC0%0/3
defined $this and ref $this || length $this
defined $this ref $this length $this
Truth table25%1/4
A and B or C
A= defined $thisB= ref $thisC= length $this
ABCresult
0XX0
1000
11X1
1011
99 {
100 0 return( $self->error( "No argument was provided." ) );
101 }
102 142 my $opts = $self->_get_args_as_hash( @_ );
103 142 ▶ $opts->{write} = 0 if( !exists( $opts->{write} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"write"} 71 71 142
104 142 ▶ my $mode = $opts->{write} ? '+>' : '<';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'write'} ? : 71 71 142
105 142 my $fh;
106 142 my $is_native_glob = 0;
107  
108 142 ▶ if( $self->_is_glob( $this ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_glob($this)) { } 58 84 142
elsif ($self->_is_scalar($this)) { } 72 12 84
109 {
110 58 $fh = $this;
111 # Even if this is an in-memory scalar handle, fileno() returns -1, which is true
112 58 ▶ $is_native_glob++ if( fileno( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
if fileno $this 58 0 58
113 }
114 elsif( $self->_is_scalar( $this ) )
115 {
116 72 ▶ $fh = Module::Generic::Scalar::IO->new( $this, $mode ) ||
Condition50%1/2
'Module::Generic::Scalar::IO'->new($this, $mode) || (return $self->pass_error('Module::Generic::Scalar::IO'->error))
l!l
720
MC/DC0%0/1
'Module::Generic::Scalar::IO'->new($this, $mode) || (return $self->pass_error('Module::Generic::Scalar::IO'->error))
'Module::Generic::Scalar::IO'->new($this, $mode)
Truth table50%1/2
A
A= 'Module::Generic::Scalar::IO'->new($this, $mode)
Aresult
11
00
117 return( $self->pass_error( Module::Generic::Scalar::IO->error ) );
118 72 $is_native_glob++;
119 }
120 else
121 {
122 12 ▶ my $f = $self->new_file( "$this" ) || return( $self->pass_error );
Condition50%1/2
$self->new_file("$this") || (return $self->pass_error)
l!l
120
MC/DC0%0/1
$self->new_file("$this") || (return $self->pass_error)
$self->new_file("$this")
Truth table50%1/2
A
A= $self->new_file("$this")
Aresult
11
00
123 12 ▶ if( !$f->exists && !$opts->{write} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($f->exists or $opts->{"write"}) 0 12 12
Condition33%1/3
$f->exists or $opts->{"write"}
l!l&&r!l&&!r
1200
MC/DC0%0/2
$f->exists or $opts->{"write"}
$f->exists $opts->{"write"}
Truth table33%1/3
A or B
A= $f->existsB= $opts->{"write"}
ABresult
1X1
011
000
124 {
125 0 return( $self->error( "File '$this' does not exist." ) );
126 }
127 12 ▶ $fh = $f->open( $mode, { binmode => 'raw', ( $opts->{write} ? ( autoflush => 1 ) : () ) } ) ||
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'write'} ? : 12 0 12
Condition50%1/2
$f->open($mode, {'binmode', 'raw', $opts->{'write'} ? ('autoflush', 1) : ()}) || (return $self->pass_error($f->error))
l!l
120
MC/DC0%0/1
$f->open($mode, {'binmode', 'raw', $opts->{'write'} ? ('autoflush', 1) : ()}) || (return $self->pass_error($f->error))
$f->open($mode, {'binmode', 'raw', $opts->{'write'} ? ('autoflush', 1) : ()})
Truth table50%1/2
A
A= $f->open($mode, {'binmode', 'raw', $opts->{'write'} ? ('autoflush', 1) : ()})
Aresult
11
00
128 return( $self->pass_error( $f->error ) );
129 12 $is_native_glob++;
130 }
131  
132 142 my $flags;
133 142 ▶ if( $self->_can( $fh => 'fcntl' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_can($fh, 'fcntl')) { } 96 46 142
134 {
135 96 $flags = $fh->fcntl( F_GETFL, 0 );
136 }
137 else
138 {
139 46 $flags = fcntl( $fh, F_GETFL, 0 );
140 }
141  
142 142 ▶ if( defined( $flags ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flags) 96 46 142
143 {
144 96 ▶ if( $opts->{write} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'write'}) { } 71 25 96
145 {
146 71 ▶ unless( $flags & ( O_RDWR | O_WRONLY | O_APPEND ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($flags & 1027) 0 71 71
147 {
148 0 return( $self->error( "Filehandle provided does not have write permission enabled." ) );
149 }
150 }
151 else
152 {
153 25 ▶ unless( ( ( $flags & O_RDONLY ) == O_RDONLY ) || ( $flags & O_RDWR ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (($flags & 0) == 0 or $flags & 2) 0 25 25
Condition33%1/3
($flags & 0) == 0 or $flags & 2
l!l&&r!l&&!r
2500
MC/DC0%0/2
($flags & 0) == 0 or $flags & 2
($flags & 0) == 0 $flags & 2
Truth table33%1/3
A or B
A= ($flags & 0) == 0B= $flags & 2
ABresult
1X1
011
000
154 {
155 0 return( $self->error( "Filehandle provided does not have read permission enabled. File handle flags value is '$flags'" ) );
156 }
157 }
158 }
159  
160 # We check if the file handle is an object, because calling core read() or print()
161 # on it would not work unless the glob has implemented a tie. See perltie.
162 142 my $op;
163 my $meth;
164 142 ▶ if( $opts->{write} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'write'}) { } 71 71 142
165 {
166 71 ▶ if( $is_native_glob )
Branch: 2 branches
BranchTrueFalseTotal
if ($is_native_glob) { } 71 0 71
elsif ($meth = $self->_can($fh, 'print') || $self->_can($fh, 'write')) { } 0 0 0
167 {
168 $op = sub
169 77 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.1
170 77 my $rv = print( $fh @_ );
171 77 ▶ return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $!" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 77 77
172 if( !defined( $rv ) );
173 77 return( $rv );
174 71 };
175 }
176 ▶ elsif( ( $meth = ( $self->_can( $fh => 'print' ) || $self->_can( $fh => 'write' ) ) ) )
Condition0%0/3
$self->_can($fh, 'print') || $self->_can($fh, 'write')
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_can($fh, 'print') || $self->_can($fh, 'write')
$self->_can($fh, 'print') $self->_can($fh, 'write')
Truth table0%0/3
A || B
A= $self->_can($fh, 'print')B= $self->_can($fh, 'write')
ABresult
1X1
011
000
177 {
178 $op = sub
179 0 ▶ {
Subroutine __ANON__: not called CC=6 SCAR=37.4
180 0 local $@;
181 0 my $rv = eval{ $fh->$meth( @_ ) };
182 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
183 {
184 0 return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $@" ) );
185 }
186 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 0 0
187 {
188 0 my $err;
189 0 ▶ if( defined( $! ) )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $!) { } 0 0 0
elsif ($self->_can($fh, 'error')) { } 0 0 0
elsif ($self->_can($fh, 'errstr')) { } 0 0 0
190 {
191 0 $err = $!;
192 }
193 elsif( $self->_can( $fh => 'error' ) )
194 {
195 0 $err = $fh->error;
196 }
197 elsif( $self->_can( $fh => 'errstr' ) )
198 {
199 0 $err = $fh->errstr;
200 }
201 0 return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $err" ) );
202 }
203 0 return( $rv );
204 0 };
205 }
206 else
207 {
208 0 return( $self->error( "The file handle provided is not a native opened one and does not support the print() or write() methods." ) );
209 }
210 }
211 else
212 {
213 71 ▶ if( $is_native_glob )
Branch: 2 branches
BranchTrueFalseTotal
if ($is_native_glob) { } 71 0 71
elsif ($self->_can($fh, 'read')) { } 0 0 0
214 {
215 $op = sub
216 47 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.6
217 47 my $n = read( $fh, $_[0], $_[1] );
218 47 ▶ if( !defined( $n ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 47 47
219 {
220 0 return( $self->error( "Error reading ", $_[1], " bytes of data from input: $!" ) );
221 }
222 47 return( $n );
223 71 };
224 }
225 elsif( $self->_can( $fh => 'read' ) )
226 {
227 $op = sub
228 0 ▶ {
Subroutine __ANON__: not called CC=6 SCAR=37.4
229 0 local $@;
230 0 my $n = eval{ $fh->read( @_ ) };
231 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
232 {
233 0 return( $self->error( "Error reading ", $_[1], " bytes of data from input: $@" ) );
234 }
235 0 ▶ if( !defined( $n ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 0 0
236 {
237 0 my $err;
238 0 ▶ if( defined( $! ) )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $!) { } 0 0 0
elsif ($self->_can($fh, 'error')) { } 0 0 0
elsif ($self->_can($fh, 'errstr')) { } 0 0 0
239 {
240 0 $err = $!;
241 }
242 elsif( $self->_can( $fh => 'error' ) )
243 {
244 0 $err = $fh->error;
245 }
246 elsif( $self->_can( $fh => 'errstr' ) )
247 {
248 0 $err = $fh->errstr;
249 }
250 0 return( $self->error( "Error reading ", $_[1], " bytes of data from input: $err" ) );
251 }
252 0 return( $n );
253 0 };
254 }
255 else
256 {
257 0 return( $self->error( "The file handle provided is not a native opened one and does not support the read() method." ) );
258 }
259 }
260 142 return( $fh, $op );
261 }
262  
263 # NOTE: sub FREEZE is inherited
264  
265 0 ▶ sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
266  
267 0 ▶ sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
268  
269 # NOTE: sub THAW is inherited
270 }
271  
272 8 1;
273 # NOTE: POD
274 __END__
« Mail/Make/Entity.pm ↑ Summary Mail/Make/Stream/QuotedPrint.pm »