blib/lib/Text/xSV.pm

filter: Statementstmt 73.5% 181 / 246 Branchbran 59.4% 82 / 138 Conditioncond 58.3% 14 / 24 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 81.8% 27 / 33 Podpod 86.9% 20 / 23 totaltotal 68.0% 326 / 479 CC 84 SCAR 22.7
CC
84
Cov
68%
format_data
30.0
_get_row
22.5
set_header
20.0
CRAP 9.7
↑ Summary
1 package Text::xSV;
2 1 $VERSION = 0.21;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶sub alias {
Subroutine alias: called CC=3 SCAR=12.0
Pod documented
7 1 my ($self, $from, $to) = @_;
8 my $field_pos = $self->{field_pos}
9 1 ▶ or return $self->error_handler(
Branch: 1 branch
BranchTrueFalseTotal
my $field_pos = $self->{"field_pos"} or return $self->error_handler("Can't call alias before headers are bound") 1 0 1
10 "Can't call alias before headers are bound");
11 1 ▶ unless (exists $field_pos->{$from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $field_pos->{$from}) 0 1 1
12 0 return $self->error_handler("'$from' is not available to alias");
13 }
14 1 $field_pos->{$to} = $field_pos->{$from};
15 }
16  
17 2 ▶sub add_compute {
Subroutine add_compute: called CC=3 SCAR=12.0
Pod documented
18 2 my ($self, $name, $compute) = @_;
19 my $field_pos = $self->{field_pos}
20 2 ▶ or return $self->error_handler(
Branch: 1 branch
BranchTrueFalseTotal
my $field_pos = $self->{"field_pos"} or return $self->error_handler("Can't call add_compute before headers are bound") 2 0 2
21 "Can't call add_compute before headers are bound");
22 2 ▶ unless (UNIVERSAL::isa($compute, "CODE")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($compute, "CODE")) 0 2 2
23 0 return $self->error_handler(
24 'Usage: $csv->add_compute("name", sub {FUNCTION});');
25 }
26 2 $field_pos->{$name} = $compute;
27 }
28  
29 1 ▶sub bind_fields {
Subroutine bind_fields: called CC=2 SCAR=6.9
Pod documented
30 1 my $self = shift;
31 1 my %field_pos;
32 1 foreach my $i (0..$#_) {
33 3 $field_pos{$_[$i]} = $i;
34 }
35 1 $self->{field_pos} = \%field_pos;
36 }
37  
38 1 ▶sub bind_header {
Subroutine bind_header: called CC=1 SCAR=0.0
Pod documented
39 1 my $self = shift;
40 1 $self->bind_fields($self->get_row());
41 }
42  
43 1 *read_headers = \&bind_header;
44 1 *read_header = \&bind_header;
45  
46 1 ▶sub delete {
Subroutine delete: called CC=4 SCAR=14.9
Pod undocumented
47 1 my $self = shift;
48 my $field_pos = $self->{field_pos}
49 1 ▶ or return $self->error_handler(
Branch: 1 branch
BranchTrueFalseTotal
my $field_pos = $self->{"field_pos"} or return $self->error_handler("Can't call delete before headers are bound") 1 0 1
50 "Can't call delete before headers are bound");
51 1 foreach my $field (@_) {
52 1 ▶ if (exists $field_pos->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field_pos->{$field}) { } 1 0 1
53 1 delete $field_pos->{$field};
54 }
55 else {
56 0 $self->error_handler(
57 "Cannot delete field '$field': it doesn't exist");
58 }
59 }
60 }
61  
62 5 ▶sub error_handler {
Subroutine error_handler: called CC=1 SCAR=0.0
Pod undocumented
63 5 my $self = shift;
64 5 $self->{error_handler}->(@_);
65 }
66  
67 25 ▶sub extract {
Subroutine extract: called CC=11 SCAR=24.0
Pod documented
68 25 my $self = shift;
69 25 ▶ my $cached_results = $self->{cached} ||= {};
Condition100%2/2
$self->{"cached"} ||= {}
l!l
1510
MC/DC100%1/1
$self->{"cached"} ||= {}
$self->{"cached"}
Truth table100%2/2
A
A= $self->{"cached"}
Aresult
11
00
70 25 ▶ my $in_compute = $self->{in_compute} ||= {};
Condition100%2/2
$self->{"in_compute"} ||= {}
l!l
1510
MC/DC100%1/1
$self->{"in_compute"} ||= {}
$self->{"in_compute"}
Truth table100%2/2
A
A= $self->{"in_compute"}
Aresult
11
00
71 25 ▶ my $row = $self->{row} or return $self->error_handler(
Branch: 1 branch
BranchTrueFalseTotal
my $row = $self->{"row"} or return $self->error_handler("No row found (did you call get_row())?") 25 0 25
72 "No row found (did you call get_row())?");
73 my $lookup = $self->{field_pos}
74 25 ▶ or return $self->error_handler(
Branch: 1 branch
BranchTrueFalseTotal
my $lookup = $self->{"field_pos"} or return $self->error_handler("Can't find field info (did you bind_fields or read_header?)") 25 0 25
75 "Can't find field info (did you bind_fields or read_header?)");
76 25 my @data;
77 25 foreach my $field (@_) {
78 45 ▶ if (exists $lookup->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lookup->{$field}) { } 43 2 45
79 43 my $position_or_compute = $lookup->{$field};
80 43 ▶ if (not ref($position_or_compute)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $position_or_compute) { } 35 8 43
elsif (exists $cached_results->{$field}) { } 1 7 8
elsif ($in_compute->{$field}) { } 2 5 7
81 35 push @data, $row->[$position_or_compute];
82 }
83 elsif (exists $cached_results->{$field}) {
84 1 push @data, $cached_results->{$field};
85 }
86 elsif ($in_compute->{$field}) {
87 2 $self->error_handler(
88 "Infinite recursion detected in computing '$field'");
89 }
90 else {
91 # Have to do compute
92 5 $in_compute->{$field} = 1;
93 5 $cached_results->{$field} = $position_or_compute->($self);
94 4 push @data, $cached_results->{$field};
95 }
96 }
97 else {
98 2 my @allowed = sort keys %$lookup;
99 2 $self->error_handler(
100 "Invalid field $field for file '$self->{filename}'.\n" .
101 "Valid fields are: (@allowed)\n"
102 );
103 }
104 }
105 21 ▶ return wantarray ? @data : \@data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 19 2 21
106 }
107  
108 4 ▶sub extract_hash {
Subroutine extract_hash: called CC=3 SCAR=11.0
Pod documented
109 4 my $self = shift;
110 4 ▶ my @fields = @_ ? @_ : $self->get_fields();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 3 4
111 4 my %hash;
112 4 @hash{@fields} = $self->extract(@fields);
113 4 ▶ wantarray ? %hash : \%hash;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
114 }
115  
116 2 ▶sub fetchrow_hash {
Subroutine fetchrow_hash: called CC=2 SCAR=6.9
Pod documented
117 2 my $self = shift;
118 2 ▶ return unless $self->get_row();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_row 1 1 2
119 1 $self->extract_hash(@_);
120 }
121  
122 0 ▶sub format_data {
Subroutine format_data: not called CC=5 SCAR=34.0
Pod documented
123 0 my $self = shift;
124 0 my %data = @_;
125 0 my @row;
126 0 ▶ my $field_pos = $self->{field_pos} or $self->error_handler(
Branch: 1 branch
BranchTrueFalseTotal
my $field_pos = $self->{"field_pos"} or $self->error_handler("Can't find field info (did you bind_fields or read_header?)") 0 0 0
127 "Can't find field info (did you bind_fields or read_header?)"
128 );
129 0 ▶ while (my ($field, $value) = each %data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %data) 0 0 0
130 0 my $pos = $field_pos->{$field};
131 0 ▶ if (defined($pos)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) { } 0 0 0
132 0 $row[$pos] = $value;
133 }
134 else {
135 0 $self->warning_handler("Ignoring unknown field '$field'");
136 }
137 }
138 0 $self->{row} = \@row;
139 my $header = $self->{header}
140 0 ▶ or $self->error_handler("Cannot format_data when no header is set");
Branch: 1 branch
BranchTrueFalseTotal
my $header = $self->{"header"} or $self->error_handler("Cannot format_data when no header is set") 0 0 0
141 0 $self->format_row( $self->extract( @$header ));
142 }
143  
144 0 ▶sub format_header {
Subroutine format_header: not called CC=2 SCAR=17.9
Pod documented
145 0 my $self = shift;
146 0 ▶ if ($self->{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'header'}) { } 0 0 0
147 0 return $self->format_row(@{$self->{header}});
148 }
149 else {
150 0 $self->error_handler("Cannot format_header when no header is set");
151 }
152 }
153  
154 1 *format_headers = \&format_header;
155  
156 4 ▶sub format_row {
Subroutine format_row: called CC=12 SCAR=25.0
Pod documented
157 4 my $self = shift;
158  
159 4 $self->{row_num}++;
160  
161 4 ▶ if ($self->{row_size_warning}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"row_size_warning"}) 4 0 4
162 4 ▶ if (not exists $self->{row_size}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $self->{'row_size'}) { } 1 3 4
elsif (@_ != $self->{'row_size'}) { } 0 3 3
163 1 $self->{row_size} = @_;
164 }
165 elsif ( @_ != $self->{row_size}) {
166 0 my $count = @_;
167 0 $self->warning_handler(
168 "Formatting $count fields at row $self->{row_num}, "
169 . "expected $self->{row_size}"
170 );
171 }
172 }
173  
174 4 my $sep = $self->{sep};
175 4 my @row;
176 4 foreach my $value (@_) {
177 16 ▶ if (not defined($value)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $value) { } 3 13 16
elsif ('' eq $value) { } 3 10 13
elsif ($value =~ /\s|\Q$sep\E|"/) { } 3 7 10
178 # Empty fields are undef
179 3 ▶ push @row, $self->{quote_all} ? qq("") : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'quote_all'} ? : 1 2 3
180 }
181 elsif ("" eq $value) {
182 # The empty string has to be quoted unless dont_quote is set
183 3 ▶ push @row, $self->{dont_quote} ? "" : qq{""};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dont_quote'} ? : 1 2 3
184 }
185 elsif ($value =~ /\s|\Q$sep\E|"/) {
186 # quote it
187 3 local $_ = $value;
188 3 s/"/""/g;
189 # If dont_quote is set, just output the data element,
190 # otherwise follow the 'proper' CSV quoted format (that breaks
191 # MS SQL Server's bulk insert on date values)
192 3 ▶ push @row, $self->{dont_quote} ? $_ : qq{"$_"};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dont_quote'} ? : 1 2 3
193 }
194 else {
195 # Unquoted is fine (that is, unless the quote_all option is set)
196 7 ▶ push @row, $self->{quote_all} ? qq("$value") : $value;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'quote_all'} ? : 1 6 7
197 }
198 }
199 4 my $row = join $sep, @row;
200 4 return $row . "\n";
201 }
202  
203 4 ▶sub get_fields {
Subroutine get_fields: called CC=2 SCAR=7.1
Pod documented
204 4 my $self = shift;
205 my $field_pos = $self->{field_pos}
206 4 ▶ or return $self->error_handler(
Branch: 1 branch
BranchTrueFalseTotal
my $field_pos = $self->{"field_pos"} or return $self->error_handler("Can't call get_fields before headers are bound") 4 0 4
207 "Can't call get_fields before headers are bound");
208 4 return keys %$field_pos;
209 }
210  
211 # Private block for shared variables in a small "parse engine".
212 # The concept here is to use pos to step through a string.
213 # This is the real engine, all else is syntactic sugar.
214 {
215 1 my ($self, $fh, $line, $is_error);
216  
217 15 ▶ sub get_row {
Subroutine get_row: called CC=11 SCAR=25.8
Pod documented
218 15 $self = shift;
219 15 $is_error = 0;
220 15 delete $self->{row};
221 15 delete $self->{cached};
222 15 delete $self->{in_compute};
223 $fh = ($self->{fh}
224 ||= $self->{filename}
225 ? $self->open_file($self->{filename}, "<")
226 15 ▶ : ($self->{filename} = "ARGV", \*ARGV)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'filename'} ? : 2 0 2
Condition66%2/3
$self->{"fh"} ||= $self->{"filename"} ? $self->open_file($self->{"filename"}, "<") : ($self->{"filename"} = "ARGV", \*ARGV)
l!l&&r!l&&!r
1320
MC/DC0%0/2
$self->{"fh"} ||= $self->{"filename"} ? $self->open_file($self->{"filename"}, "<") : ($self->{"filename"} = "ARGV", \*ARGV)
$self->{"fh"} $self->{"filename"} ? $self->open_file($self->{"filename"}, "<") : ($self->{"filename"} = "ARGV", \*ARGV)
Truth table66%2/3
A ||= B
A= $self->{"fh"}B= $self->{"filename"} ? $self->open_file($self->{"filename"}, "<") : ($self->{"filename"} = "ARGV", \*ARGV)
ABresult
1X1
011
000
227 # Sorry for the above convoluted way to sneak in defining filename.
228 );
229 15 ▶ return unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 15 15
230 15 ▶ defined($line = <$fh>) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined($line = readline $fh) or return 13 2 15
231 13 ▶ if ($self->{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filter"}) 13 0 13
232 13 $line = $self->{filter}->($line);
233 }
234 13 chomp($line);
235 13 my @row = _get_row();
236 13 ▶ if ($is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_error) 0 13 13
237 0 return @row[0..$#row];
238 }
239 13 ▶ if (not exists $self->{row_size}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $self->{'row_size'}) { } 2 11 13
elsif (not $self->{'row_size_warning'}) { } 0 11 11
elsif ($self->{'row_size'} != @row) { } 0 11 11
240 2 $self->{row_size} = @row;
241 }
242 elsif (not $self->{row_size_warning}) {
243 # The user asked not to get this warning, so don't issue it.
244 }
245 elsif ($self->{row_size} != @row) {
246 0 my $new = @row;
247 0 my $where = "Line $., file $self->{filename}";
248 0 $self->warning_handler(
249 "$where had $new fields, expected $self->{row_size}" );
250 }
251 13 $self->{row} = \@row;
252 13 ▶ return wantarray ? @row : [@row];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 12 13
253 }
254  
255 13 ▶ sub _get_row {
Subroutine _get_row: called CC=10 SCAR=31.1
256 13 my @row;
257 13 my $q_sep = quotemeta($self->{sep});
258 13 my $match_sep = qr/\G$q_sep/;
259 13 my $start_field = qr/\G(")/;
260 13 my $start_field_ms = qr/\G([^"$q_sep]*)/;
261  
262 # This loop is the heart of the engine
263 13 ▶ while ($line =~ /$start_field/gc or $line =~ /$start_field_ms/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$start_field/cg or $line =~ /$start_field_ms/cg) 41 0 41
Condition66%2/3
$line =~ /$start_field/cg or $line =~ /$start_field_ms/cg
l!l&&r!l&&!r
16250
MC/DC0%0/2
$line =~ /$start_field/cg or $line =~ /$start_field_ms/cg
$line =~ /$start_field/cg $line =~ /$start_field_ms/cg
Truth table66%2/3
A or B
A= $line =~ /$start_field/cgB= $line =~ /$start_field_ms/cg
ABresult
1X1
011
000
264 41 ▶ if ($1 eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '"') { } 16 25 41
265 16 push @row, _get_quoted();
266 }
267 else {
268 # Needed for Microsoft compatibility
269 25 ▶ push @row, length($1) ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 21 4 25
270 }
271 41 my $pos = pos($line);
272 41 ▶ if ($line !~ /$match_sep/g) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /$match_sep/g) 13 28 41
273 13 ▶ if ($pos == length($line)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pos == length $line) { } 13 0 13
elsif ($self->{'strict'}) { } 0 0 0
274 13 return @row;
275 }
276 elsif ($self->{strict}) {
277 0 my $expected = "Expected '$self->{sep}'";
278 0 $is_error = 1;
279 0 return $self->error_handler(
280 "$expected at $self->{filename}, line $., char $pos");
281 }
282 else {
283 TRY: {
284 0 my $expected = "Expected '$self->{sep}'";
285 0 $self->warning_handler(
286 "$expected at $self->{filename}, line $., char $pos");
287  
288 # Assume we are in non-strict mode and encountered a single "
289 # so we need to recover and finish my quoted field.
290 0 $row[-1] .= '"' . _get_quoted();
291 0 $pos = pos($line);
292 0 ▶ if ($line !~ /$match_sep/g) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /$match_sep/g) 0 0 0
293 0 ▶ if ($pos == length($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos == length $line) { } 0 0 0
294 0 return @row;
295 }
296 else {
297 0 redo TRY;
298 }
299 }
300 }
301 }
302 }
303 }
304 0 $is_error = 1;
305 0 $self->error_handler(
306 "I have no idea how parsing $self->{filename} left me here!");
307 }
308  
309 16 ▶ sub _get_quoted {
Subroutine _get_quoted: called CC=6 SCAR=18.3
310 16 my $piece = "";
311 16 my $start_line = $.;
312 16 my $start_pos = pos($line);
313
314 16 while(1) {
315 32 ▶ if ($line =~ /\G([^"]+)/gc) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /\G([^"]+)/cg) { } 14 18 32
elsif ($line =~ /\G""/cg) { } 1 17 18
elsif ($line =~ /\G"/g) { } 16 1 17
316 # sequence of non-quote characters
317 14 $piece .= $1;
318 } elsif ($line =~ /\G""/gc) {
319 # replace "" with "
320 1 $piece .= '"';
321 } elsif ($line =~ /\G"/g) {
322 # closing quote
323 16 return $piece; # EXIT HERE
324 }
325 else {
326 # Must be at end of line
327 1 $piece .= $/;
328 1 ▶ unless(defined($line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($line = readline $fh)) 0 1 1
329 0 croak(
330 "File $self->{filename} ended inside a quoted field\n"
331 . "Field started at char $start_pos, line $start_line\n"
332 );
333 }
334 1 ▶ if ($self->{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filter"}) 1 0 1
335 1 $line = $self->{filter}->($line);
336 }
337 1 chomp($line);
338 }
339 }
340 0 $is_error = 1;
341 0 $self->error_handler(
342 "I have no idea how parsing $self->{filename} left me here!");
343 }
344 }
345  
346 1 my @normal_accessors = qw(
347 close_fh error_handler warning_handler filename filter fh
348 row_size row_size_warning strict
349 );
350 1 foreach my $accessor (@normal_accessors) {
351 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
352 9 ▶ *{"set_$accessor"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
353 25 $_[0]->{$accessor} = $_[1];
354 9 };
355 }
356  
357 # These two are mutually exclusive
358 1 foreach my $accessor (qw(dont_quote quote_all)) {
359 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
360 2 ▶ *{"set_$accessor"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.8
361 3 my $self = shift;
362 3 $self->{$accessor} = shift;
363 3 ▶ if ($self->{dont_quote} and $self->{quote_all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dont_quote"} and $self->{"quote_all"}) 0 3 3
Condition66%2/3
$self->{"dont_quote"} and $self->{"quote_all"}
!ll&&!rl&&r
210
MC/DC0%0/2
$self->{"dont_quote"} and $self->{"quote_all"}
$self->{"dont_quote"} $self->{"quote_all"}
Truth table66%2/3
A and B
A= $self->{"dont_quote"}B= $self->{"quote_all"}
ABresult
0X0
100
111
364 0 $self->error_handler("Can't set both dont_quote and quote_all");
365 }
366 2 };
367 }
368  
369 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
370 3 my $self = bless ({}, shift);
371 my %allowed = map {
372 3 $_=>1
373 } @normal_accessors, qw(
374 header headers row sep dont_quote quote_all
375 );
376  
377 my %args = (
378 error_handler => \&confess,
379 14 ▶ filter => sub {my $line = shift; $line =~ s/\r$//; $line;},
Subroutine __ANON__: called CC=1 SCAR=0.0
380 3 sep => ",",
381 row_size_warning => 1,
382 close_fh => 0,
383 strict => 1,
384 @_
385 );
386 # Note, must set error_handler and warning_handler first because they
387 # might get called while processing the other args.
388 3 foreach my $arg ('error_handler', 'warning_handler', keys %args) {
389 27 ▶ unless (exists $allowed{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $allowed{$arg}) 0 27 27
390 0 my @allowed = sort keys %allowed;
391 0 croak("Invalid argument '$arg', allowed args: (@allowed)");
392 }
393 27 my $method = "set_$arg";
394 27 $self->$method($args{$arg});
395 }
396 3 return $self;
397 }
398  
399 # Note the undocumented third argument for the mode. Most of the time this
400 # will do what is wanted without requiring Perl 5.6 or better. Users who
401 # supply their own metacharacters will also not be surprised at the result.
402 # Note the return of 0. I cannot assume that the user's error handler dies...
403 3 ▶sub open_file {
Subroutine open_file: called CC=5 SCAR=17.8
Pod documented
404 3 my $self = shift;
405 3 ▶ my $file = $self->{filename} = shift || return $self->error_handler(
Condition50%1/2
shift() || (return $self->error_handler('No filename specified at open_file'))
l!l
30
MC/DC0%0/1
shift() || (return $self->error_handler('No filename specified at open_file'))
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
406 "No filename specified at open_file"
407 );
408 3 ▶ if ($file !~ /\||<|>/ and @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file =~ /\||<|>/ and @_) 3 0 3
Condition33%1/3
not $file =~ /\||<|>/ and @_
!ll&&!rl&&r
003
MC/DC0%0/2
not $file =~ /\||<|>/ and @_
not $file =~ /\||<|>/ @_
Truth table33%1/3
not(A) and B
A= not $file =~ /\||<|>/B= @_
ABresult
0X0
100
111
409 3 my $mode = shift;
410 3 $file = "$mode $file";
411 }
412 3 my $fh = do {local *FH}; # Old trick, not needed in 5.6
413 3 ▶ unless (open ($fh, $file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, $file) 0 3 3
414 0 $self->error_handler("Cannot open '$file': $!");
415 0 return 0;
416 }
417 3 $self->{close_fh} = 1;
418 3 $self->{fh} = $fh;
419 }
420  
421 4 ▶sub print {
Subroutine print: called CC=5 SCAR=17.2
Pod documented
422 4 my $self = shift;
423 4 $self->{row_out}++;
424 my $fh = ($self->{fh}
425 ||= $self->{filename}
426 ? $self->open_file($self->{filename}, ">")
427 4 ▶ : ($self->{filename} = "STDOUT", \*STDOUT)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'filename'} ? : 1 0 1
Condition66%2/3
$self->{"fh"} ||= $self->{"filename"} ? $self->open_file($self->{"filename"}, ">") : ($self->{"filename"} = "STDOUT", \*STDOUT)
l!l&&r!l&&!r
310
MC/DC0%0/2
$self->{"fh"} ||= $self->{"filename"} ? $self->open_file($self->{"filename"}, ">") : ($self->{"filename"} = "STDOUT", \*STDOUT)
$self->{"fh"} $self->{"filename"} ? $self->open_file($self->{"filename"}, ">") : ($self->{"filename"} = "STDOUT", \*STDOUT)
Truth table66%2/3
A ||= B
A= $self->{"fh"}B= $self->{"filename"} ? $self->open_file($self->{"filename"}, ">") : ($self->{"filename"} = "STDOUT", \*STDOUT)
ABresult
1X1
011
000
428 # Sorry for the above convoluted way to sneak in defining filename.
429 );
430 4 ▶ return unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 4 4
431 4 ▶ print $fh @_ or $self->error_handler( "Print #$self->{row_out}: $!" );
Branch: 1 branch
BranchTrueFalseTotal
print $fh @_ or $self->error_handler(qq[Print #$self->{"row_out"}: $!]) 4 0 4
432 }
433  
434 0 ▶sub print_data {
Subroutine print_data: not called CC=1 SCAR=6.9
Pod documented
435 0 my $self = shift;
436 0 $self->print($self->format_data(@_));
437 }
438  
439 0 ▶sub print_header {
Subroutine print_header: not called CC=1 SCAR=6.9
Pod documented
440 0 my $self = shift;
441 0 $self->print($self->format_header(@_));
442 }
443  
444 1 *print_headers = \&print_header;
445  
446 4 ▶sub print_row {
Subroutine print_row: called CC=1 SCAR=0.0
Pod documented
447 4 my $self = shift;
448 4 $self->print($self->format_row(@_));
449 }
450  
451 0 ▶sub set_header {
Subroutine set_header: not called CC=4 SCAR=30.0
Pod documented
452 0 my $self = shift;
453 0 ▶ if (1 == @_ and UNIVERSAL::isa($_[0], 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @_ and UNIVERSAL::isa($_[0], 'ARRAY')) { } 0 0 0
Condition0%0/3
1 == @_ and UNIVERSAL::isa($_[0], "ARRAY")
!ll&&!rl&&r
000
MC/DC0%0/2
1 == @_ and UNIVERSAL::isa($_[0], "ARRAY")
1 == @_ UNIVERSAL::isa($_[0], "ARRAY")
Truth table0%0/3
A and B
A= 1 == @_B= UNIVERSAL::isa($_[0], "ARRAY")
ABresult
0X0
100
111
454 0 $self->{header} = $_[0];
455 }
456 else {
457 0 $self->{header} = \@_;
458 }
459 0 ▶ if (not exists $self->{field_pos}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"field_pos"}) 0 0 0
460 0 $self->bind_fields(@{$self->{header}});
461 }
462 }
463  
464 1 *set_headers = \&set_header;
465  
466 5 ▶sub set_sep {
Subroutine set_sep: called CC=2 SCAR=6.9
Pod documented
467 5 my $self = shift;
468 5 my $sep = shift;
469 # The reason for this limitation is so that $start_field in _get_row
470 # will do what it is supposed to. (I should use a negative lookahead,
471 # but I'm documenting this late at night and want some sleep.)
472 5 ▶ if (1 == length($sep)) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == length $sep) { } 4 1 5
473 4 $self->{sep} = $sep;
474 }
475 else {
476 1 $self->error_handler("The separator '$sep' is not of length 1");
477 }
478 }
479  
480 0 ▶sub warning_handler {
Subroutine warning_handler: not called CC=3 SCAR=24.8
Pod undocumented
481 0 my $self = shift;
482 0 ▶ if ($self->{warning_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'warning_handler'}) { } 0 0 0
483 0 $self->{warning_handler}->(@_);
484 }
485 else {
486 0 eval { $self->{error_handler}->(@_) };
487 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
488 }
489 }
490  
491 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=16.9
492 2 my $self = shift;
493 2 ▶ if ($self->{close_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"close_fh"}) 2 0 2
494 2 ▶ close($self->{fh}) or $self->error_handler(
Branch: 2 branches
BranchTrueFalseTotal
close $self->{"fh"} or $self->error_handler($! ? qq[Cannot close '$self->{"filename"}': $!] : qq[Exit status $? closing '$self->{"filename"}']) 2 0 2
$! ? : 0 0 0
495 $! ? "Cannot close '$self->{filename}': $!"
496 : "Exit status $? closing '$self->{filename}'"
497 );
498 }
499 }
500  
501 1 1;
502  
503 __END__
↑ Summary