| 1 | package PICA::PlainParser; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | $PICA::PlainParser::VERSION = '0.585'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | #ABSTRACT: Parse normalized PICA+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use PICA::Field; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use PICA::Record; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 65 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 65 | my ($class, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 65 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | field_handler => defined $params{Field} ? $params{Field} : undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | record_handler => defined $params{Record} ? $params{Record} : undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | broken_field_handler => defined $params{FieldError} ? $params{FieldError} : undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | broken_record_handler => defined $params{RecordError} ? $params{RecordError} : undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | proceed => $params{Proceed} ? $params{Proceed} : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | limit => ($params{Limit} || 0) * 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | offset => ($params{Offset} || 0) * 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | record => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | broken => undef, # broken record | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | read_records => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 65 | ▶ | 'strict' => $params{strict} || 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition83%5/6
$params{'Limit'} || 0
$params{'Offset'} || 0
$params{'strict'} || 0
MC/DC100%1/1
$params{'Limit'} || 0
$params{'Limit'}
MC/DC0%0/1
$params{'Offset'} || 0
$params{'Offset'}
MC/DC100%1/1
$params{'strict'} || 0
$params{'strict'}
Truth table100%2/2
A
A= $params{'Limit'}
Truth table50%1/2
A
A= $params{'Offset'}
Truth table100%2/2
A
A= $params{'strict'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | filename => "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | fields => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | read_counter => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | active => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 65 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 32 | ▶ | sub parsefile { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parsefile: called CC=19 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 32 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 32 | ▶ | if ( ref($file) eq 'GLOB' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | $self->{filehandle} = $file; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | $self->{filename} = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } elsif ( UNIVERSAL::isa( $file, 'IO::Handle' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 8 | $self->{filehandle} = $file; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 8 | $self->{filename} = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 19 | $self->{filename} = $file; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 19 | my $fh = $file; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 19 | ▶ | $fh = "zcat $fh |" if $fh =~ /\.gz$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 19 | ▶ | $fh = "unzip -p $fh |" if $fh =~ /\.zip$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 19 | ▶ | $self->{filehandle} = IO::File->new($file, '<:utf8') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | or croak("failed to open file $file"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 32 | PICA::Parser::enable_binmode_encoding( $self->{filehandle} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 32 | ▶ | if ( not $self->{proceed} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 32 | $self->{read_counter} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 32 | $self->{read_records} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 32 | $self->{active} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 32 | $self->{record} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 32 | my $dumpformat = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 32 | my $line = readline( $self->{filehandle} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 32 | ▶ | if ($line =~ /\x1E/) { # dumpformat useds \x1E instead of newlines | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | my $EOL = $/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 5 | $/ = chr(0x1E); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | my $id = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 5 | my @linebuf = split( /\x1E/, $line ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 5 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 422 | ▶ | last if ($self->finished()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 422 | ▶ | if (@linebuf) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 417 | $line = shift @linebuf; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 417 | ▶ | if (defined $line and not @linebuf) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $line and not @linebuf
MC/DC50%1/2
defined $line and not @linebuf
defined $line
not @linebuf
Truth table66%2/3
A and not(B)
A= defined $lineB= not @linebuf
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 5 | $line .= readline( $self->{filehandle} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 5 | $line = readline( $self->{filehandle} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 422 | ▶ | if ( defined $line ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 417 | $line =~ /^\x1D?([^\s]*)/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 417 | ▶ | if (PICA::Field::parse_pp_tag($1)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 392 | $self->_parseline($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 25 | ▶ | if ( "$id" ne "$1" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 12 | $self->_parseline(""); # next record | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 25 | $id = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } while(defined $line); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 5 | $/ = $EOL; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 27 | ▶ | while ( defined $line and not $self->finished ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $line and not $self->finished
MC/DC50%1/2
defined $line and not $self->finished
defined $line
not $self->finished
Truth table66%2/3
A and not(B)
A= defined $lineB= not $self->finished
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3593 | $self->_parseline($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3593 | $line = readline( $self->{filehandle} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 32 | ▶ | $self->handle_record() unless $self->finished(); # handle last record | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 32 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 39 | ▶ | sub parsedata { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parsedata: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 39 | my ($self, $data, $additional) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 39 | $self->{active} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 39 | $self->{record} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 39 | ▶ | if ( ! $self->{proceed} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 37 | $self->{read_counter} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 37 | $self->{read_records} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 39 | ▶ | if ( ref($data) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | my $chunk = &$data(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | while(defined $chunk) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 27 | $self->_parsedata($chunk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 27 | $chunk = &$data(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } elsif( UNIVERSAL::isa( $data, "PICA::Record" ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # re-parse the record (could obviously be speed up by dropping tests) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | foreach ( $data->fields ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 26 | $self->_parseline( $_->string ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 37 | $self->_parsedata($data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 39 | $self->handle_record(); # handle last record | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 39 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 17 | ▶ | sub records { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
records: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 17 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 17 | return @{ $self->{read_records} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 3354 | ▶ | sub counter { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
counter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 3354 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 3354 | return $self->{read_counter}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 4047 | ▶ | sub finished { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
finished: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 4047 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 4047 | ▶ | return $self->{limit} && $self->counter() >= $self->{limit}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{'limit'} && $self->counter >= $self->{'limit'}
MC/DC0%0/2
$self->{'limit'} && $self->counter >= $self->{'limit'}
$self->{'limit'}
$self->counter >= $self->{'limit'}
Truth table66%2/3
A && B
A= $self->{'limit'}B= $self->counter >= $self->{'limit'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 64 | ▶ | sub _parsedata { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parsedata: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 64 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 64 | my @lines; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 64 | ▶ | if (ref(\$data) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 63 | ▶ | @lines = $data eq "\n" ? ('') : split "\n", $data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } elsif (ref($data) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | @lines = @{$data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | croak("Got " . ref(\$data) . " when parsing PICA+ while expecting SCALAR or ARRAY"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 64 | foreach my $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 458 | $self->_parseline($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 4481 | ▶ | sub _parseline { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parseline: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 4481 | my ($self, $line) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 4481 | chomp $line; # remove newline if present | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # start of record marker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 4481 | ▶ | if ( $line eq "\x1D" or (not $self->{strict} and $line =~ /^\s*$|^#|^SET/) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$line eq "\35" or not $self->{"strict"} and $line =~ /^\s*$|^#|^SET/
not $self->{"strict"} and $line =~ /^\s*$|^#|^SET/
MC/DC100%3/3
$line eq "\35" or not $self->{"strict"} and $line =~ /^\s*$|^#|^SET/
$line eq "\35"
not $self->{"strict"}
$line =~ /^\s*$|^#|^SET/
Truth table100%4/4
A or not(B) and C
A= $line eq "\35"B= not $self->{"strict"}C= $line =~ /^\s*$|^#|^SET/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 61 | ▶ | $self->handle_record() if $self->{active} and @{$self->{fields}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"active"} and @{$self->{"fields"};}
MC/DC100%2/2
$self->{"active"} and @{$self->{"fields"};}
$self->{"active"}
@{$self->{"fields"};}
Truth table100%3/3
A and B
A= $self->{"active"}B= @{$self->{"fields"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 4420 | $line =~ s/^\x1D//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 4420 | my $field = eval { PICA::Field->parse($line); }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 4420 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 17 | $@ =~ s/ at .*\n//; # remove line number | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 17 | $field = $self->broken_field( $@, $line ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } elsif ($self->{field_handler}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 3111 | $field = $self->{field_handler}( $field ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 4420 | ▶ | if ( UNIVERSAL::isa( $field, 'PICA::Field' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1293 | push (@{$self->{fields}}, $field); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } elsif ( defined $field ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 4 | ▶ | $self->{broken} = $field unless defined $self->{broken}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 4481 | $self->{active} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 17 | ▶ | sub broken_field { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
broken_field: called CC=3 SCAR=16.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 17 | my ($self, $msg, $line) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 17 | ▶ | if ($self->{broken_field_handler}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 17 | return $self->{broken_field_handler}( $msg, $line ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | $msg = "$msg in line \"$line\"" if defined $line; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | print STDERR "$msg\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # TODO: count/collect errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 13 | ▶ | sub broken_record { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
broken_record: called CC=5 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 13 | my ($self, $msg, $record) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 13 | ▶ | if ($self->{broken_record_handler}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 4 | return $self->{broken_record_handler}( $msg, $record ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 9 | ▶ | return if UNIVERSAL::isa( $record, 'PICA::Record' ) && $record->empty; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
UNIVERSAL::isa($record, "PICA::Record") and $record->empty
MC/DC0%0/2
UNIVERSAL::isa($record, "PICA::Record") and $record->empty
UNIVERSAL::isa($record, "PICA::Record")
$record->empty
Truth table33%1/3
A and B
A= UNIVERSAL::isa($record, "PICA::Record")B= $record->empty
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | print STDERR "$msg\n" if defined $msg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 92 | ▶ | sub handle_record { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_record: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 92 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 92 | $self->{read_counter}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 92 | my ($record, $broken); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # $self->{broken} = "empty record" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # unless defined $self->{broken} or @{$self->{fields}} > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 92 | ▶ | if ( $self->{broken} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 2 | $broken = $self->{broken}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 90 | $record = PICA::Record->new( @{$self->{fields}} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 92 | $self->{fields} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 92 | $self->{broken} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # TODO: fix this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | # return if ($self->{offset} && $self->{read_counter} < $self->{offset}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 92 | ▶ | if (not defined $broken) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 90 | ▶ | if ($self->{record_handler}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 28 | ▶ | if (UNIVERSAL::isa( $self->{record_handler}, 'PICA::Writer') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 7 | $self->{record_handler}->write( $record ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | #$record = TODO allow here! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 21 | $record = $self->{record_handler}( $record ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 21 | ▶ | $record = undef if $record =~ /^-?\d+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 90 | ▶ | if (defined $record) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 82 | ▶ | if ( UNIVERSAL::isa( $record, 'PICA::Record' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 81 | ▶ | $broken = "empty record" if $record->empty; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 1 | $broken = $record; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 92 | ▶ | if ( defined $broken ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 13 | $self->broken_record( $broken, $record ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } elsif ( defined $record ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 71 | push @{ $self->{read_records} }, $record; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||