| 1 | package MAB2::Parser::Disk; | ||||||||||||||||||||||||||||||||
| 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 $SUBFIELD_INDICATOR => qq{\N{INFORMATION SEPARATOR ONE}}; | |||||||||||||||||||||||||||||||
| 12 | 6 | Readonly my $END_OF_FIELD => qq{\N{LINE FEED}}; | |||||||||||||||||||||||||||||||
| 13 | 6 | Readonly my $END_OF_RECORD => q{}; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 16 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||
| 17 | 4 | my $file = shift; | |||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 4 | my $self = { | |||||||||||||||||||||||||||||||
| 20 | filename => undef, | ||||||||||||||||||||||||||||||||
| 21 | rec_number => 0, | ||||||||||||||||||||||||||||||||
| 22 | reader => undef, | ||||||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | # check for file or filehandle | ||||||||||||||||||||||||||||||||
| 26 | 4 | my $ishandle = eval { fileno($file); }; | |||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | 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
| |||||||||||||||||||||||||||||||||
| 28 | 1 | $self->{filename} = scalar $file; | |||||||||||||||||||||||||||||||
| 29 | 1 | $self->{reader} = $file; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | elsif ( -e $file ) { | ||||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | open $self->{reader}, '<:encoding(UTF-8)', $file | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 33 | or croak "cannot read from file $file\n"; | ||||||||||||||||||||||||||||||||
| 34 | 2 | $self->{filename} = $file; | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||||||
| 37 | 1 | croak "file or filehande $file does not exists"; | |||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | 3 | return ( bless $self, $class ); | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 27 | ▶ | sub next { | ||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 43 | 27 | my $self = shift; | |||||||||||||||||||||||||||||||
| 44 | 27 | local $/ = $END_OF_RECORD; | |||||||||||||||||||||||||||||||
| 45 | 27 | ▶ | if ( my $data = $self->{reader}->getline() ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 26 | $self->{rec_number}++; | |||||||||||||||||||||||||||||||
| 47 | 26 | my $record = _decode($data); | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | # get last subfield from 001 as id | ||||||||||||||||||||||||||||||||
| 50 | 26 | my ($id) = map { $_->[-1] } grep { $_->[0] =~ '001' } @{$record}; | |||||||||||||||||||||||||||||||
| 51 | 26 | return { _id => $id, record => $record }; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | 1 | return; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 26 | ▶ | sub _decode { | ||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 57 | 26 | my $reader = shift; | |||||||||||||||||||||||||||||||
| 58 | 26 | chomp($reader); | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 26 | my @record; | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 26 | my @fields = split( $END_OF_FIELD, $reader ); | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 26 | my $leader = shift @fields; | |||||||||||||||||||||||||||||||
| 65 | 26 | ▶ | if ($leader =~ m/^\N{NUMBER SIGN}{3}\s(\d{5}[cdnpu]M2.0\d{7}\s{6}\w)/xms ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||||||
| 67 | 25 | push( @record, [ 'LDR', '', '_', $1 ] ); | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||||||
| 70 | 1 | carp "faulty record leader: $leader"; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 26 | foreach my $field (@fields) { | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 1055 | ▶ | if ( length $field <= 4 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 1 | carp "faulty field: \"$field\""; | |||||||||||||||||||||||||||||||
| 77 | 1 | next; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 1054 | ▶ | if ( my ( $tag, $ind, $data ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | = $field =~ m/^(\d{3})([A-Za-z0-9\s])(.*)/ ) | ||||||||||||||||||||||||||||||||
| 82 | { | ||||||||||||||||||||||||||||||||
| 83 | # check if data contains subfield indicators | ||||||||||||||||||||||||||||||||
| 84 | 1053 | ▶ | if ( $data =~ m/\s*($SUBFIELD_INDICATOR|\$)(.*)/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 61 | ▶ | my $subfield_indicator = $1 eq '$' ? '\$' : $1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | push | ||||||||||||||||||||||||||||||||
| 87 | @record, | ||||||||||||||||||||||||||||||||
| 88 | [ | ||||||||||||||||||||||||||||||||
| 89 | $tag, | ||||||||||||||||||||||||||||||||
| 90 | $ind, | ||||||||||||||||||||||||||||||||
| 91 | 61 | map { ( substr( $_, 0, 1 ), substr( $_, 1 ) ) } | |||||||||||||||||||||||||||||||
| 92 | split /$subfield_indicator/, | ||||||||||||||||||||||||||||||||
| 93 | $2 | ||||||||||||||||||||||||||||||||
| 94 | ]; | ||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||||||
| 97 | 992 | push @record, [ $tag, $ind, '_', $data ]; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||||||||||||||
| 101 | 1 | carp "faulty field structure: \"$field\""; | |||||||||||||||||||||||||||||||
| 102 | 1 | next; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | 26 | return \@record; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 6 | 1; # End of MAB2::Parser::Disk | |||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||||||