| 1 | package MARC::Parser::RAW; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = "0.06"; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use charnames qw< :full >; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Encode qw(find_encoding); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use English; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 2 | Readonly my $LEADER_LEN => 24; | |||||||||||||||||||||||||||||||
| 16 | 2 | Readonly my $SUBFIELD_INDICATOR => qq{\N{INFORMATION SEPARATOR ONE}}; | |||||||||||||||||||||||||||||||
| 17 | 2 | Readonly my $END_OF_FIELD => qq{\N{INFORMATION SEPARATOR TWO}}; | |||||||||||||||||||||||||||||||
| 18 | 2 | Readonly my $END_OF_RECORD => qq{\N{INFORMATION SEPARATOR THREE}}; | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | MARC::Parser::RAW - Parser for ISO 2709 encoded MARC records | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | =begin markdown | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | [](https://travis-ci.org/jorol/MARC-Parser-RAW) | ||||||||||||||||||||||||||||||||
| 27 | [](https://coveralls.io/r/jorol/MARC-Parser-RAW?branch=devel) | ||||||||||||||||||||||||||||||||
| 28 | [](http://cpants.cpanauthors.org/dist/MARC-Parser-RAW) | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | =end markdown | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | use MARC::Parser::RAW; | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | my $parser = MARC::Parser::RAW->new( $file ); | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | while ( my $record = $parser->next() ) { | ||||||||||||||||||||||||||||||||
| 39 | # do something | ||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | L<MARC::Parser::RAW> is a lightweight, fault tolerant parser for ISO 2709 | ||||||||||||||||||||||||||||||||
| 45 | encoded MARC records. Tags, indicators and subfield codes are not validated | ||||||||||||||||||||||||||||||||
| 46 | against the MARC standard. Record length from leader and field lengths from | ||||||||||||||||||||||||||||||||
| 47 | the directory are ignored. Records with a faulty structure will be skipped | ||||||||||||||||||||||||||||||||
| 48 | with a warning. The resulting data structure is optimized for usage with the | ||||||||||||||||||||||||||||||||
| 49 | L<Catmandu> data tool kit. | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | L<MARC::Parser::RAW> expects UTF-8 encoded files as input. Otherwise provide | ||||||||||||||||||||||||||||||||
| 52 | a filehande with a specified I/O layer or specify encoding. | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | =head1 MARC | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | The MARC record is parsed into an ARRAY of ARRAYs: | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | $record = [ | ||||||||||||||||||||||||||||||||
| 59 | [ 'LDR', undef, undef, '_', '00661nam 22002538a 4500' ], | ||||||||||||||||||||||||||||||||
| 60 | [ '001', undef, undef, '_', 'fol05865967 ' ], | ||||||||||||||||||||||||||||||||
| 61 | ... | ||||||||||||||||||||||||||||||||
| 62 | [ '245', '1', '0', 'a', 'Programming Perl /', | ||||||||||||||||||||||||||||||||
| 63 | 'c', 'Larry Wall, Tom Christiansen & Jon Orwant.' | ||||||||||||||||||||||||||||||||
| 64 | ], | ||||||||||||||||||||||||||||||||
| 65 | ... | ||||||||||||||||||||||||||||||||
| 66 | ]; | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | =head2 new($file|$fh|$scalarref [, $encoding]) | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =head3 Configuration | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | =over | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | =item C<file> | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | Path to file with raw MARC records. | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | =item C<fh> | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | Open filehandle for raw MARC records. | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | =item C<scalarref> | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | Reference to scalar with raw MARC records. | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | =item C<encoding> | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | Set encoding. Default: UTF-8. Optional. | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | =back | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 98 | 8 | my ( $class, $file, $encoding ) = @_; | |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 8 | ▶ | $file or croak "first argument must be a file or filehandle"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 7 | ▶ | if ($encoding) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | 2 | ▶ | find_encoding($encoding) or croak "encoding \"$_[0]\" not found"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 6 | ▶ | my $self = { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | file => undef, | ||||||||||||||||||||||||||||||||
| 108 | fh => undef, | ||||||||||||||||||||||||||||||||
| 109 | encoding => $encoding ? $encoding : 'UTF-8', | ||||||||||||||||||||||||||||||||
| 110 | rec_number => 0, | ||||||||||||||||||||||||||||||||
| 111 | }; | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | # check for file or filehandle | ||||||||||||||||||||||||||||||||
| 114 | # ToDo: check for scalar ref | ||||||||||||||||||||||||||||||||
| 115 | 6 | my $ishandle = eval { fileno($file); }; | |||||||||||||||||||||||||||||||
| 116 | 6 | ▶ | if ( !$@ && defined $ishandle ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not $@ and defined $ishandle
MC/DC100%2/2
not $@ and defined $ishandle
not $@
defined $ishandle
Truth table100%3/3
not(A) and B
A= not $@B= defined $ishandle
| |||||||||||||||||||||||||||||||||
| 117 | 1 | $self->{file} = scalar $file; | |||||||||||||||||||||||||||||||
| 118 | 1 | $self->{fh} = $file; | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | ▶ | elsif ( -e $file || ref($file) eq 'SCALAR' ) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
-e $file or ref $file eq "SCALAR"
MC/DC100%2/2
-e $file or ref $file eq "SCALAR"
-e $file
ref $file eq "SCALAR"
Truth table100%3/3
A or B
A= -e $fileB= ref $file eq "SCALAR"
| |||||||||||||||||||||||||||||||||
| 121 | 4 | ▶ | open $self->{fh}, "<:encoding($self->{encoding})", $file | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | or croak "cannot read from file $file\n"; | ||||||||||||||||||||||||||||||||
| 123 | 4 | $self->{file} = $file; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | else { | ||||||||||||||||||||||||||||||||
| 126 | 1 | croak "file or filehande $file does not exists"; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | 5 | return ( bless $self, $class ); | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | =head2 next() | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | Reads the next record from MARC input stream. Returns a Perl hash. | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 6 | ▶ | sub next { | ||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 138 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||
| 139 | 6 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||
| 140 | 6 | local $INPUT_RECORD_SEPARATOR = $END_OF_RECORD; | |||||||||||||||||||||||||||||||
| 141 | 6 | ▶ | if ( defined( my $raw = <$fh> ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | 6 | $self->{rec_number}++; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | # remove illegal garbage that sometimes occurs between records | ||||||||||||||||||||||||||||||||
| 145 | 6 | $raw | |||||||||||||||||||||||||||||||
| 146 | =~ s/^[\N{SPACE}\N{NUL}\N{LINE FEED}\N{CARRIAGE RETURN}\N{SUB}]+//; | ||||||||||||||||||||||||||||||||
| 147 | 6 | ▶ | return unless $raw; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 6 | ▶ | if ( my $marc = $self->_decode($raw) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | 2 | return $marc; | |||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | else { | ||||||||||||||||||||||||||||||||
| 153 | 4 | return $self->next(); | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | 0 | return; | |||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | =head2 _decode($record) | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | Deserialize a raw MARC record to an ARRAY of ARRAYs. | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 6 | ▶ | sub _decode { | ||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 166 | 6 | my ( $self, $raw ) = @_; | |||||||||||||||||||||||||||||||
| 167 | 6 | chop $raw; | |||||||||||||||||||||||||||||||
| 168 | 6 | my ( $head, @fields ) = split $END_OF_FIELD, $raw; | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 6 | ▶ | if ( !@fields ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 171 | 1 | carp "no fields found in record " . $self->{rec_number}; | |||||||||||||||||||||||||||||||
| 172 | 1 | return; | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | # ToDO: better RegEX for leader | ||||||||||||||||||||||||||||||||
| 176 | 5 | my $leader; | |||||||||||||||||||||||||||||||
| 177 | 5 | ▶ | if ( $head =~ /(.{$LEADER_LEN})/cg ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 4 | $leader = $1; | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||||||
| 181 | 1 | carp "no valid record leader found in record " . $self->{rec_number}; | |||||||||||||||||||||||||||||||
| 182 | 1 | return; | |||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | 4 | my @tags = $head =~ /\G(\d{3})\d{9}/cg; | |||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 4 | ▶ | if ( scalar @tags != scalar @fields ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 188 | carp "different number of tags and fields in record " | ||||||||||||||||||||||||||||||||
| 189 | 1 | . $self->{rec_number}; | |||||||||||||||||||||||||||||||
| 190 | 1 | return; | |||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 3 | ▶ | if ( $head !~ /\G$/cg ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 194 | 1 | carp "incomplete directory entry in record " . $self->{rec_number}; | |||||||||||||||||||||||||||||||
| 195 | 1 | return; | |||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | return [ | ||||||||||||||||||||||||||||||||
| 199 | 2 | [ 'LDR', undef, undef, '_', $leader ], | |||||||||||||||||||||||||||||||
| 200 | map [ shift(@tags), $self->_field($_) ], | ||||||||||||||||||||||||||||||||
| 201 | @fields | ||||||||||||||||||||||||||||||||
| 202 | ]; | ||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | =head2 _field($field) | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | Split MARC field string in individual components. | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | 35 | ▶ | sub _field { | ||||||||||||||||||||||||||||||
|
Subroutine
_field: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 212 | 35 | my ( $self, $field ) = @_; | |||||||||||||||||||||||||||||||
| 213 | 35 | my @chunks = split( /$SUBFIELD_INDICATOR(.)/, $field ); | |||||||||||||||||||||||||||||||
| 214 | 35 | ▶ | return ( undef, undef, '_', @chunks ) if @chunks == 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | 27 | my @subfields; | |||||||||||||||||||||||||||||||
| 216 | 27 | my ( $indicator1, $indicator2 ) = ( split //, shift @chunks ); | |||||||||||||||||||||||||||||||
| 217 | 27 | ▶ | while (@chunks) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 218 | 47 | push @subfields, ( splice @chunks, 0, 2 ); | |||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | 27 | return ( $indicator1, $indicator2, @subfields ); | |||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | Johann Rolschewski E<lt>jorol@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | Copyright 2014- Johann Rolschewski | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | =head1 LICENSE | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||
| 234 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | =head1 SEEALSO | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | L<Catmandu>, L<Catmandu::Importer::MARC>. | ||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | =head1 ACKNOWLEDGEMENT | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | The parser methods are adapted from Marc Chantreux's L<MARC::MIR> module. | ||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 2 | 1; # End of MARC::Parser::RAW | |||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||