| 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
| |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 47 | my $self = bless \%options, $class; | |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 47 | ▶ | my $fh = $self->{fh} // \*STDOUT; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"fh"} // \*STDOUT
MC/DC0%0/1
$self->{"fh"} // \*STDOUT
$self->{"fh"}
Truth table50%1/2
A
A= $self->{"fh"}
| |||||||||||||||||||||||||||||||||||
| 19 | 47 | ▶ | if (!ref $fh) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | if (open(my $handle, '>:encoding(UTF-8)', $fh)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||||||||
| 28 | 47 | ▶ | if (length $$fh) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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')
builtin::blessed($fh) and $fh->can("print")
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")
| |||||||||||||||||||||||||||||||||||
| 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"} // {}
MC/DC0%0/1
$self->{"color"} // {}
$self->{"color"}
Truth table50%1/2
A
A= $self->{"color"}
| |||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 56 | ▶ | $fh->print($col{tag} ? colored($field->[0], $col{tag}) : $field->[0]); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | 56 | ▶ | if ($field->[1] > 0) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 66 | $record, | ||||||||||||||||||||||||||||||||||
| 67 | allow_empty_subfields => 1, | ||||||||||||||||||||||||||||||||||
| 68 | ignore_empty_records => 1 | ||||||||||||||||||||||||||||||||||
| 69 | ) or return; | ||||||||||||||||||||||||||||||||||
| 70 | 44 | ▶ | return unless @$record; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$self->{"annotate"} // @$field % 2
MC/DC0%0/1
$self->{"annotate"} // @$field % 2
$self->{"annotate"}
Truth table50%1/2
A
A= $self->{"annotate"}
| |||||||||||||||||||||||||||||||||||
| 93 | 12 | ▶ | return @$field % 2 ? $field->[$#$field] : " "; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 16 | 1; | |||||||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||||||||