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