PICA/Writer/Base.pm

filter: Statementstmt 87.8% 58 / 66 Branchbran 56.2% 18 / 32 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 93.3% 14 / 15 Podpod 44.4% 4 / 9 totaltotal 67.5% 98 / 145 CC 25 SCAR 24.9
CC
25
Cov
71%
new
34.5
write_identifier
6.4
annotation
4.2
CRAP 12.1
« PICA/Schema/Builder.pm ↑ Summary PICA/Patch.pm »
1 package PICA::Writer::Base;
2 16 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 16 our $VERSION = '2.12';
5  
6 16 ▶use Scalar::Util qw(blessed openhandle reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use PICA::Schema qw(clean_pica);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 47 ▶sub new {
Subroutine new: called CC=11 SCAR=35.4
Pod documented
13 47 my $class = shift;
14 47 ▶ my (%options) = @_ % 2 ? (fh => @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 47 47
15  
16 47 my $self = bless \%options, $class;
17  
18 47 ▶ my $fh = $self->{fh} // \*STDOUT;
Condition50%1/2
$self->{"fh"} // \*STDOUT
l!l
470
MC/DC0%0/1
$self->{"fh"} // \*STDOUT
$self->{"fh"}
Truth table50%1/2
A
A= $self->{"fh"}
Aresult
11
00
19 47 ▶ if (!ref $fh) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $fh) { } 0 47 47
elsif (builtin::reftype($fh) eq 'SCALAR' and not builtin::blessed($fh)) { } 47 0 47
elsif (not &openhandle($fh) || builtin::blessed($fh) && $fh->can('print')) { } 0 0 0
20 0 ▶ if (open(my $handle, '>:encoding(UTF-8)', $fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $handle, '>:encoding(UTF-8)', $fh) { } 0 0 0
21 0 $fh = $handle;
22 }
23 else {
24 0 croak "cannot open file for writing: $fh\n";
25 }
26 }
27 ▶ elsif (reftype $fh eq 'SCALAR' and !blessed $fh) {
Condition33%1/3
builtin::reftype($fh) eq "SCALAR" and not builtin::blessed($fh)
!ll&&!rl&&r
0047
MC/DC0%0/2
builtin::reftype($fh) eq "SCALAR" and not builtin::blessed($fh)
builtin::reftype($fh) eq "SCALAR" not builtin::blessed($fh)
Truth table33%1/3
A and not(B)
A= builtin::reftype($fh) eq "SCALAR"B= not builtin::blessed($fh)
ABresult
0X0
100
111
28 47 ▶ if (length $$fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$fh) 0 47 47
29 0 $$fh = decode("UTF-8", $$fh);
30 }
31 47 open(my $handle, '>>:encoding(UTF-8)', $fh);
32 47 $fh = $handle;
33 }
34 ▶ elsif (!openhandle($fh) and !(blessed $fh && $fh->can('print'))) {
Condition0%0/6
&openhandle($fh) || builtin::blessed($fh) && $fh->can('print')
l!l&&r!l&&!r
000
builtin::blessed($fh) and $fh->can("print")
!ll&&!rl&&r
000
MC/DC0%0/3
&openhandle($fh) || builtin::blessed($fh) && $fh->can('print')
&openhandle($fh) builtin::blessed($fh) $fh->can("print")
Truth table0%0/4
A || B and C
A= &openhandle($fh)B= builtin::blessed($fh)C= $fh->can("print")
ABCresult
1XX1
0111
00X0
0100
35 0 croak 'expect filehandle or object with method print!';
36 }
37 47 $self->{fh} = $fh;
38  
39 47 $self;
40 }
41  
42 47 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
Pod documented
43 47 my $self = shift;
44 47 $self->write_record($_) for @_;
45 47 $self;
46 }
47  
48 56 ▶sub write_identifier {
Subroutine write_identifier: called CC=6 SCAR=18.6
Pod undocumented
49 56 my ($self, $field) = @_;
50  
51 56 my $fh = $self->{fh};
52 56 ▶ my %col = %{$self->{color} // {}};
Condition50%1/2
$self->{"color"} // {}
l!l
056
MC/DC0%0/1
$self->{"color"} // {}
$self->{"color"}
Truth table50%1/2
A
A= $self->{"color"}
Aresult
11
00
53  
54 56 ▶ $fh->print($col{tag} ? colored($field->[0], $col{tag}) : $field->[0]);
Branch: 1 branch
BranchTrueFalseTotal
$col{'tag'} ? : 0 56 56
55  
56 56 ▶ if ($field->[1] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[1] > 0) 3 53 56
57 3 my $occ = sprintf("%02d", $field->[1]);
58 $fh->print(($col{syntax} ? colored('/', $col{syntax}) : '/')
59 3 ▶ . ($col{occurrence} ? colored($occ, $col{occurrence}) : $occ));
Branch: 2 branches
BranchTrueFalseTotal
$col{'syntax'} ? : 0 3 3
$col{'occurrence'} ? : 0 3 3
60 }
61 }
62  
63 44 ▶sub write_record {
Subroutine write_record: called CC=4 SCAR=13.9
Pod documented
64 44 my ($self, $record) = @_;
65 44 ▶ $record = clean_pica(
Branch: 1 branch
BranchTrueFalseTotal
$record = clean_pica($record, "allow_empty_subfields", 1, "ignore_empty_records", 1) or return 44 0 44
66 $record,
67 allow_empty_subfields => 1,
68 ignore_empty_records => 1
69 ) or return;
70 44 ▶ return unless @$record;
Branch: 1 branch
BranchTrueFalseTotal
unless @$record 3 41 44
71  
72 41 my $fh = $self->{fh};
73 41 $self->write_field($_) for @$record;
74 41 $fh->print($self->END_OF_RECORD);
75 }
76  
77 56 ▶sub write_field {
Subroutine write_field: called CC=2 SCAR=6.9
Pod undocumented
78 56 my ($self, $field) = @_;
79  
80 56 $self->write_start_field($field);
81  
82 56 ▶ for (my $i = 3; $i < scalar @$field; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$field) 59 56 115
83 59 $self->write_subfield($field->[$i - 1], $field->[$i]);
84 }
85  
86 56 $self->{fh}->print($self->END_OF_FIELD);
87 }
88  
89 56 ▶sub annotation {
Subroutine annotation: called CC=4 SCAR=14.3
Pod undocumented
90 56 my ($self, $field) = @_;
91  
92 56 ▶ return unless $self->{annotate} // @$field % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"annotate"} // @$field % 2 44 12 56
Condition50%1/2
$self->{"annotate"} // @$field % 2
l!l
056
MC/DC0%0/1
$self->{"annotate"} // @$field % 2
$self->{"annotate"}
Truth table50%1/2
A
A= $self->{"annotate"}
Aresult
11
00
93 12 ▶ return @$field % 2 ? $field->[$#$field] : " ";
Branch: 1 branch
BranchTrueFalseTotal
@$field % 2 ? : 12 0 12
94 }
95  
96 0 ▶sub write_start_field {
Subroutine write_start_field: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my ($self, $field) = @_;
98  
99 # ignore annotation by default
100 0 $self->write_identifier($field);
101 0 $self->{fh}->print(' ');
102 }
103  
104 2 ▶sub write_subfield {
Subroutine write_subfield: called CC=1 SCAR=0.0
Pod undocumented
105 2 my ($self, $code, $value) = @_;
106 2 $self->{fh}->print($self->SUBFIELD_INDICATOR . $code . $value);
107 }
108  
109 46 ▶sub end {
Subroutine end: called CC=2 SCAR=7.2
Pod documented
110 46 my $self = shift;
111 46 ▶ close $self->{fh} if $self->{fh} ne \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh"} ne \*STDOUT 46 0 46
112 }
113  
114 16 1;
115 __END__
« PICA/Schema/Builder.pm ↑ Summary PICA/Patch.pm »