Serialize/Role/Sink/Stream.pm

filter: Statementstmt 94.7% 54 / 57 Branchbran 80.0% 24 / 30 Conditioncond 55.5% 15 / 27 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 77.8% 109 / 140 CC 25 SCAR 23.2
CC
25
Cov
82%
_build_fh
15.3
__ANON__
12.4
close
4.0
CRAP 10.2
« Serialize/Role/Default.pm ↑ Summary Serialize/Util.pm »
1 package Data::Record::Serialize::Role::Sink::Stream;
2  
3 # ABSTRACT: output encoded data to a stream.
4  
5 10 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use Data::Record::Serialize::Error { errors => [ '::create', '::parameter', '::internal' ] }, -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Types::Standard qw[ Bool ];
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 our $VERSION = '2.05';
13  
14 10 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 ## no critic( NamingConventions::ProhibitAmbiguousNames )
19 ## no critic( Subroutines::ProhibitBuiltinHomonyms )
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50 has output => (
51 is => 'ro',
52 default => q{-},
53 21 ▶ isa => sub {
Subroutine __ANON__: called CC=10 SCAR=25.2
54 21 ▶ defined $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] or error("::parameter", "'output' parameter must be defined") 21 0 21
55 or error( '::parameter', q{'output' parameter must be defined} );
56 21 my $ref = ref $_[0];
57 21 ▶ return if $ref eq 'GLOB' or $ref eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "GLOB" or $ref eq "SCALAR" 13 8 21
Condition100%3/3
$ref eq "GLOB" or $ref eq "SCALAR"
l!l&&r!l&&!r
1128
MC/DC100%2/2
$ref eq "GLOB" or $ref eq "SCALAR"
$ref eq "GLOB" $ref eq "SCALAR"
Truth table100%3/3
A or B
A= $ref eq "GLOB"B= $ref eq "SCALAR"
ABresult
1X1
011
000
58  
59 8 ▶ if ( $ref eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "") 6 2 8
60 6 $ref = ref( \$_[0] ); # turn plain *STDOUT into \*STDOUT
61 6 ▶ return if $ref eq 'GLOB';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "GLOB" 1 5 6
62 5 ▶ return if length $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] 5 0 5
63 0 error( '::parameter', q{string 'output' parameter must not be empty} );
64 }
65  
66 return
67 2 ▶ if Scalar::Util::blessed $_[0]
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($_[0]) and $_[0]->isa("IO::Handle") || $_[0]->isa("FileHandle") 2 0 2
Condition33%2/6
builtin::blessed($_[0]) and $_[0]->isa("IO::Handle") || $_[0]->isa("FileHandle")
!ll&&!rl&&r
002
$_[0]->isa("IO::Handle") or $_[0]->isa("FileHandle")
l!l&&r!l&&!r
200
MC/DC0%0/3
builtin::blessed($_[0]) and $_[0]->isa("IO::Handle") || $_[0]->isa("FileHandle")
builtin::blessed($_[0]) $_[0]->isa("IO::Handle") $_[0]->isa("FileHandle")
Truth table25%1/4
A and B or C
A= builtin::blessed($_[0])B= $_[0]->isa("IO::Handle")C= $_[0]->isa("FileHandle")
ABCresult
0XX0
1000
11X1
1011
68 and $_[0]->isa( 'IO::Handle' ) || $_[0]->isa( 'FileHandle' );
69 0 error( '::parameter', q{illegal value for 'output' parameter} );
70 },
71 10 );
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85 10 has fh => (
86 is => 'lazy',
87 init_arg => undef,
88 clearer => 1,
89 predicate => 1,
90 handles => ['flush'],
91 );
92  
93  
94  
95  
96  
97  
98  
99 10 has _passed_fh => (
100 is => 'rwp',
101 init_arg => undef,
102 default => 1,
103 );
104  
105 19 ▶sub _build_fh {
Subroutine _build_fh: called CC=13 SCAR=27.3
106 19 my $self = shift;
107  
108 19 my $output = $self->output;
109 19 my $ref = ref $output;
110  
111 # filename
112 19 ▶ if ( $ref eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "") 4 15 19
113 4 ▶ return $output if ref( \$output ) eq 'GLOB';
Branch: 1 branch
BranchTrueFalseTotal
if ref \$output eq "GLOB" 1 3 4
114 3 ▶ return \*STDOUT if $output eq q{-};
Branch: 1 branch
BranchTrueFalseTotal
if $output eq "-" 0 3 3
115  
116 3 $self->_set__passed_fh( 0 );
117 3 ▶ if ( $self->create_output_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->create_output_dir) 1 2 3
118 1 require Path::Tiny;
119 1 my $dir = Path::Tiny::path( $output )->parent;
120 1 ▶ eval { $dir->mkdir; } or error( '::create', "unable to create output directory '$dir': $@" );
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dir->mkdir } } or error("::create", "unable to create output directory '${dir}': $@") 1 0 1
121 }
122 return (
123 3 ▶ IO::File->new( $output, 'w' )
Condition33%1/3
'IO::File'->new($output, 'w') || error('::create', "unable to create output file: '${output}'")
l!l&&r!l&&!r
300
MC/DC0%0/2
'IO::File'->new($output, 'w') || error('::create', "unable to create output file: '${output}'")
'IO::File'->new($output, 'w') error('::create', "unable to create output file: '${output}'")
Truth table33%1/3
A || B
A= 'IO::File'->new($output, 'w')B= error('::create', "unable to create output file: '${output}'")
ABresult
1X1
011
000
124 or error( '::create', "unable to create output file: '$output'" ) );
125 }
126  
127 15 ▶ return $output
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "GLOB" or builtin::blessed($output) and $output->isa("IO::Handle") || $output->isa("FileHandle") 3 12 15
Condition66%6/9
$ref eq "GLOB" or builtin::blessed($output) and $output->isa("IO::Handle") || $output->isa("FileHandle")
l!l&&r!l&&!r
1212
builtin::blessed($output) and $output->isa("IO::Handle") || $output->isa("FileHandle")
!ll&&!rl&&r
1202
$output->isa("IO::Handle") or $output->isa("FileHandle")
l!l&&r!l&&!r
200
MC/DC50%2/4
$ref eq "GLOB" or builtin::blessed($output) and $output->isa("IO::Handle") || $output->isa("FileHandle")
$ref eq "GLOB" builtin::blessed($output) $output->isa("IO::Handle") $output->isa("FileHandle")
Truth table60%3/5
A or B and C or D
A= $ref eq "GLOB"B= builtin::blessed($output)C= $output->isa("IO::Handle")D= $output->isa("FileHandle")
ABCDresult
1XXX1
011X1
01011
00XX0
01000
128 if $ref eq 'GLOB'
129 or Scalar::Util::blessed( $output )
130 && ( $output->isa( 'IO::Handle' ) || $output->isa( 'FileHandle' ) );
131  
132 12 $self->_set__passed_fh( 0 );
133 return (
134 12 ▶ IO::File->new( $output, 'w' )
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "SCALAR" 12 0 12
Condition33%1/3
'IO::File'->new($output, 'w') || error('::create', 'unable to open scalar for output')
l!l&&r!l&&!r
1200
MC/DC0%0/2
'IO::File'->new($output, 'w') || error('::create', 'unable to open scalar for output')
'IO::File'->new($output, 'w') error('::create', 'unable to open scalar for output')
Truth table33%1/3
A || B
A= 'IO::File'->new($output, 'w')B= error('::create', 'unable to open scalar for output')
ABresult
1X1
011
000
135 or error( '::create', q{unable to open scalar for output} ) ) if $ref eq 'SCALAR';
136  
137 0 error( '::internal', q{can't get here} );
138 }
139  
140  
141  
142  
143  
144  
145  
146  
147 10 has create_output_dir => (
148 is => 'ro',
149 isa => Bool,
150 default => 0,
151 );
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177 23 ▶sub close {
Subroutine close: called CC=4 SCAR=13.9
Pod documented
178 23 my ( $self, $in_global_destruction ) = @_;
179  
180 # don't bother closing the FH in global destruction (it'll be done
181 # on its own) or if we were passed a file handle in the output
182 # attribute.
183 23 ▶ return if $in_global_destruction or $self->_passed_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $in_global_destruction or $self->_passed_fh 10 13 23
Condition66%2/3
$in_global_destruction or $self->_passed_fh
l!l&&r!l&&!r
01013
MC/DC50%1/2
$in_global_destruction or $self->_passed_fh
$in_global_destruction $self->_passed_fh
Truth table66%2/3
A or B
A= $in_global_destructionB= $self->_passed_fh
ABresult
1X1
011
000
184  
185 # fh is lazy, so the object may close without every using it, so
186 # don't inadvertently create it.
187 13 ▶ $self->fh->close if $self->has_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_fh 11 2 13
188 13 $self->clear_fh;
189 }
190  
191 10 1;
192  
193 #
194 # This file is part of Data-Record-Serialize
195 #
196 # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
197 #
198 # This is free software, licensed under:
199 #
200 # The GNU General Public License, Version 3, June 2007
201 #
202  
203 __END__
« Serialize/Role/Default.pm ↑ Summary Serialize/Util.pm »