| 1 | package MAB2::Parser::RAW; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 6 | our $VERSION = '0.24'; | |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use charnames qw< :full >; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 6 | Readonly my $LEADER_LEN => 24; | |||||||||||||||||||||||||||||||
| 12 | 6 | Readonly my $SUBFIELD_INDICATOR => qq{\N{INFORMATION SEPARATOR ONE}}; | |||||||||||||||||||||||||||||||
| 13 | 6 | Readonly my $END_OF_FIELD => qq{\N{INFORMATION SEPARATOR TWO}}; | |||||||||||||||||||||||||||||||
| 14 | 6 | Readonly my $END_OF_RECORD => qq{\N{INFORMATION SEPARATOR THREE}}; | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 17 | 5 | my $class = shift; | |||||||||||||||||||||||||||||||
| 18 | 5 | my $file = shift; | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 5 | my $self = { | |||||||||||||||||||||||||||||||
| 21 | filename => undef, | ||||||||||||||||||||||||||||||||
| 22 | rec_number => 0, | ||||||||||||||||||||||||||||||||
| 23 | reader => undef, | ||||||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # check for file or filehandle | ||||||||||||||||||||||||||||||||
| 27 | 5 | my $ishandle = eval { fileno($file); }; | |||||||||||||||||||||||||||||||
| 28 | 5 | ▶ | if ( !$@ && defined $ishandle ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
not $@ and defined $ishandle
MC/DC50%1/2
not $@ and defined $ishandle
not $@
defined $ishandle
Truth table66%2/3
not(A) and B
A= not $@B= defined $ishandle
| |||||||||||||||||||||||||||||||||
| 29 | 2 | $self->{filename} = scalar $file; | |||||||||||||||||||||||||||||||
| 30 | 2 | $self->{reader} = $file; | |||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | elsif ( -e $file ) { | ||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | open $self->{reader}, '<:encoding(UTF-8)', $file | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | or croak "cannot read from file $file\n"; | ||||||||||||||||||||||||||||||||
| 35 | 2 | $self->{filename} = $file; | |||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | else { | ||||||||||||||||||||||||||||||||
| 38 | 1 | croak "file or filehande $file does not exists"; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | 4 | return ( bless $self, $class ); | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 48 | ▶ | sub next { | ||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 44 | 48 | my $self = shift; | |||||||||||||||||||||||||||||||
| 45 | 48 | ▶ | if ( my $line = $self->{reader}->getline() ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 46 | $self->{rec_number}++; | |||||||||||||||||||||||||||||||
| 47 | 46 | my $record = _decode($line); | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | # get last subfield from 001 as id | ||||||||||||||||||||||||||||||||
| 50 | 46 | my ($id) = map { $_->[-1] } grep { $_->[0] =~ '001' } @{$record}; | |||||||||||||||||||||||||||||||
| 51 | 46 | return { _id => $id, record => $record }; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | 2 | return; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 46 | ▶ | sub _decode { | ||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 57 | 46 | my $reader = shift; | |||||||||||||||||||||||||||||||
| 58 | 46 | chomp $reader; | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 46 | ▶ | if ( substr( $reader, -1, 1 ) ne $END_OF_RECORD ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 1 | carp "record terminator not found"; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 46 | my @record; | |||||||||||||||||||||||||||||||
| 65 | 46 | my $leader = substr $reader, 0, $LEADER_LEN; | |||||||||||||||||||||||||||||||
| 66 | 46 | ▶ | if ( $leader =~ m/(\d{5}\wM2.0\d*\s*\w)/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | 45 | push @record, [ 'LDR', '', '_', $leader ]; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||||||
| 70 | 1 | carp "faulty record leader: \"$leader\""; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 46 | my @fields = split $END_OF_FIELD, substr( $reader, $LEADER_LEN, -1 ); | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 46 | for my $field (@fields) { | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 2039 | ▶ | if ( length $field <= 4 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 1 | carp "faulty field: \"$field\""; | |||||||||||||||||||||||||||||||
| 79 | 1 | next; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 2038 | ▶ | if ( my ( $tag, $ind, $data ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | = $field =~ m/^(\d{3})([A-Za-z0-9\s])(.*)/ ) | ||||||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||||||
| 85 | 2037 | ▶ | if ( $data =~ m/\s*$SUBFIELD_INDICATOR(.*)/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | push( | ||||||||||||||||||||||||||||||||
| 87 | @record, | ||||||||||||||||||||||||||||||||
| 88 | [ $tag, | ||||||||||||||||||||||||||||||||
| 89 | $ind, | ||||||||||||||||||||||||||||||||
| 90 | 109 | map { ( substr( $_, 0, 1 ), substr( $_, 1 ) ) } | |||||||||||||||||||||||||||||||
| 91 | split /$SUBFIELD_INDICATOR/, | ||||||||||||||||||||||||||||||||
| 92 | $1 | ||||||||||||||||||||||||||||||||
| 93 | ] | ||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||||||
| 97 | 1928 | push @record, [ $tag, $ind, '_', $data ]; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||||||||||||||
| 101 | 1 | carp "faulty field structure: \"$field\""; | |||||||||||||||||||||||||||||||
| 102 | 1 | next; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 46 | return \@record; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 6 | 1; # End of MAB2::Parser::RAW | |||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||||||