| 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
| |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 9 | my $self = bless \%options, $class; | |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 9 | ▶ | 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 | 9 | ▶ | 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 | 9 | ▶ | if (length $$fh) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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')
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 | 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"} // {}
MC/DC0%0/1
$self->{"color"} // {}
$self->{"color"}
Truth table50%1/2
A
A= $self->{"color"}
| |||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 7 | ▶ | $fh->print($col{tag} ? colored($field->[0], $col{tag}) : $field->[0]); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | if (defined $field->[1] and $field->[1] ne '') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $field->[1] and $field->[1] ne ""
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 ""
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | 8 | 1; | |||||||||||||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||||||||||||||