blib/lib/PICA/Writer/Base.pm

filter: Statementstmt 85.0% 51 / 60 Branchbran 34.6% 9 / 26 Conditioncond 25.0% 4 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 84.6% 11 / 13 Podpod 57.1% 4 / 7 totaltotal 60.3% 79 / 131 CC 22 SCAR 27.2
CC
22
Cov
63%
new
34.5
write_identifier
14.0
end
6.0
CRAP 15.1
« blib/lib/PICA/Parser/Plain.pm ↑ Summary blib/lib/PICA/Path.pm »
1 package PICA::Writer::Base;
2 8 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 8 our $VERSION = '1.18';
5  
6 8 ▶use Scalar::Util qw(blessed openhandle reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use PICA::Schema qw(clean_pica);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 9 ▶ my $class = shift;
Subroutine new: called CC=11 SCAR=35.4
Pod documented
14 9 ▶ my (%options) = @_ % 2 ? (fh => @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 9 9
15  
16 9 my $self = bless \%options, $class;
17  
18 9 ▶ my $fh = $self->{fh} // \*STDOUT;
Condition50%1/2
$self->{"fh"} // \*STDOUT
l!l
90
MC/DC0%0/1
$self->{"fh"} // \*STDOUT
$self->{"fh"}
Truth table50%1/2
A
A= $self->{"fh"}
Aresult
11
00
19 9 ▶ if (!ref $fh) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $fh) { } 0 9 9
elsif (builtin::reftype($fh) eq 'SCALAR' and not builtin::blessed($fh)) { } 9 0 9
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
009
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 9 ▶ if (length $$fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$fh) 0 9 9
29 0 $$fh = decode("UTF-8", $$fh);
30 }
31 9 open(my $handle, '>>:encoding(UTF-8)', $fh);
32 9 $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 9 $self->{fh} = $fh;
38  
39 9 $self;
40 }
41  
42 sub write {
43 9 ▶ my $self = shift;
Subroutine write: called CC=2 SCAR=6.9
Pod documented
44 9 $self->write_record($_) for @_;
45 9 $self;
46 }
47  
48 sub write_identifier {
49 7 ▶ my ($self, $field) = @_;
Subroutine write_identifier: called CC=7 SCAR=26.4
Pod undocumented
50  
51 7 my $fh = $self->{fh};
52 7 ▶ my %col = %{$self->{color} // {}};
Condition50%1/2
$self->{"color"} // {}
l!l
07
MC/DC0%0/1
$self->{"color"} // {}
$self->{"color"}
Truth table50%1/2
A
A= $self->{"color"}
Aresult
11
00
53  
54 7 ▶ $fh->print($col{tag} ? colored($field->[0], $col{tag}) : $field->[0]);
Branch: 1 branch
BranchTrueFalseTotal
$col{'tag'} ? : 0 7 7
55  
56 7 ▶ if (defined $field->[1] and $field->[1] ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->[1] and $field->[1] ne "") 0 7 7
Condition33%1/3
defined $field->[1] and $field->[1] ne ""
!ll&&!rl&&r
070
MC/DC0%0/2
defined $field->[1] and $field->[1] ne ""
defined $field->[1] $field->[1] ne ""
Truth table33%1/3
A and B
A= defined $field->[1]B= $field->[1] ne ""
ABresult
0X0
100
111
57 0 my $occ = sprintf("%02d", $field->[1]);
58 $fh->print(($col{syntax} ? colored('/', $col{syntax}) : '/')
59 0 ▶ . ($col{occurrence} ? colored($occ, $col{occurrence}) : $occ));
Branch: 2 branches
BranchTrueFalseTotal
$col{'syntax'} ? : 0 0 0
$col{'occurrence'} ? : 0 0 0
60 }
61 }
62  
63 sub write_record {
64 7 ▶ my ($self, $record) = @_;
Subroutine write_record: called CC=3 SCAR=11.1
Pod documented
65 7 ▶ $record = clean_pica($record, allow_empty_subfields => 1,) or return;
Branch: 1 branch
BranchTrueFalseTotal
$record = clean_pica($record, "allow_empty_subfields", 1) or return 0 7 7
66  
67 7 my $fh = $self->{fh};
68 7 $self->write_field($_) for @$record;
69 7 $fh->print($self->END_OF_RECORD);
70 }
71  
72 sub write_field {
73 7 ▶ my ($self, $field) = @_;
Subroutine write_field: called CC=2 SCAR=6.9
Pod undocumented
74  
75 7 my $fh = $self->{fh};
76  
77 7 $self->write_annotation($field);
78 7 $self->write_identifier($field);
79 7 $fh->print(' ');
80 7 ▶ for (my $i = 3; $i < scalar @$field; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$field) 7 7 14
81 7 $self->write_subfield($field->[$i - 1], $field->[$i]);
82 }
83  
84 7 $fh->print($self->END_OF_FIELD);
85 }
86  
87 ▶sub write_annotation {
Subroutine write_annotation: not called CC=1 SCAR=0.0
Pod undocumented
88  
89 # ignore field annotation by default
90 }
91  
92 sub end {
93 0 ▶ my $self = shift;
Subroutine end: not called CC=2 SCAR=17.9
Pod documented
94 0 ▶ close $self->{fh} if $self->{fh} ne \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh"} ne \*STDOUT 0 0 0
95 }
96  
97 8 1;
98 __END__
« blib/lib/PICA/Parser/Plain.pm ↑ Summary blib/lib/PICA/Path.pm »