blib/lib/MARC/Parser/RAW.pm

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