| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Catmandu::Importer::MARC::Line - Package that imports Index Data's MARC Line records | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | # From the command line | ||
| 8 | $ catmandu convert MARC --type Line --fix 'marc_map("245a","title")' < t/code4lib.line | ||
| 9 | $ catmandu convert MARC --type Line to MARC --type XML < t/code4lib.line | ||
| 10 | |||
| 11 | |||
| 12 | # From perl | ||
| 13 | use Catmandu; | ||
| 14 | |||
| 15 | # import records from file | ||
| 16 | my $importer = Catmandu->importer('MARC',file => 't/code4lib.line' , type => 'Line'); | ||
| 17 | my $fixer = Catmandu->fixer("marc_map('245a','title')"); | ||
| 18 | |||
| 19 | $importer->each(sub { | ||
| 20 | my $item = shift; | ||
| 21 | ... | ||
| 22 | }); | ||
| 23 | |||
| 24 | # or using the fixer | ||
| 25 | |||
| 26 | $fixer->fix($importer)->each(sub { | ||
| 27 | my $item = shift; | ||
| 28 | printf "title: %s\n" , $item->{title}; | ||
| 29 | }); | ||
| 30 | |||
| 31 | =head1 CONFIGURATION | ||
| 32 | |||
| 33 | =over | ||
| 34 | |||
| 35 | =item file | ||
| 36 | |||
| 37 | Read input from a local file given by its path. Alternatively a scalar | ||
| 38 | reference can be passed to read from a string. | ||
| 39 | |||
| 40 | =item fh | ||
| 41 | |||
| 42 | Read input from an L<IO::Handle>. If not specified, L<Catmandu::Util::io> is used to | ||
| 43 | create the input stream from the C<file> argument or by using STDIN. | ||
| 44 | |||
| 45 | =item encoding | ||
| 46 | |||
| 47 | Binmode of the input stream C<fh>. Set to C<:utf8> by default. | ||
| 48 | |||
| 49 | =item fix | ||
| 50 | |||
| 51 | An ARRAY of one or more fixes or file scripts to be applied to imported items. | ||
| 52 | |||
| 53 | =back | ||
| 54 | |||
| 55 | =head1 METHODS | ||
| 56 | |||
| 57 | Every Catmandu::Importer is a Catmandu::Iterable all its methods are inherited. | ||
| 58 | |||
| 59 | =head1 AUTHOR | ||
| 60 | |||
| 61 | Johann Rolschewski, E<lt>jorol at cpanE<gt> | ||
| 62 | |||
| 63 | =head1 SEE ALSO | ||
| 64 | |||
| 65 | L<Catmandu::Importer>, | ||
| 66 | L<Catmandu::Iterable> | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | package Catmandu::Importer::MARC::Line; | ||
| 71 | 2 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 72 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 73 | |||
| 74 | 2 | our $VERSION = '1.33'; | |
| 75 | |||
| 76 | 2 | with 'Catmandu::Importer'; | |
| 77 | |||
| 78 | 3 | ▶ | sub generator { |
|
Subroutine
generator: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 79 | 3 | my ($self) = @_; | |
| 80 | sub { | ||
| 81 | state $fh = $self->fh; | ||
| 82 | state $count = 0; | ||
| 83 | |||
| 84 | # set input record separator to paragraph mode | ||
| 85 | local $/ = ''; | ||
| 86 | |||
| 87 | # get next record | ||
| 88 | while (defined(my $data = $fh->getline)) { | ||
| 89 | $count++; | ||
| 90 | my @record; | ||
| 91 | my $id; | ||
| 92 | chomp $data; | ||
| 93 | |||
| 94 | # split record into fields | ||
| 95 | my @fields = split /\n/, $data; | ||
| 96 | |||
| 97 | # first field should be the MARC leader | ||
| 98 | my $leader = shift @fields; | ||
| 99 | if (length $leader == 24 && $leader =~ m/^\d{5}.*4500/) { | ||
| 100 | push @record, ['LDR', ' ', ' ', '_', $leader]; | ||
| 101 | } else { | ||
| 102 | warn "not a valid MARC leader: $leader"; | ||
| 103 | } | ||
| 104 | for my $field (@fields) { | ||
| 105 | |||
| 106 | # process control fields | ||
| 107 | if ($field =~ m/^00.\s/) { | ||
| 108 | my ($tag, $value) = $field =~ m/^(\d{3})\s(.*)/; | ||
| 109 | push @record, [$tag, ' ', ' ', '_', $value]; | ||
| 110 | |||
| 111 | # get record id | ||
| 112 | if ($tag eq '001') { | ||
| 113 | $id = $value; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | # process variable data fields | ||
| 118 | else { | ||
| 119 | my ($tag, $ind1, $ind2, $sf) | ||
| 120 | = $field =~ m/^(\d{3})\s([a-z0-9\s])([a-z0-9\s])\s(.*)/; | ||
| 121 | |||
| 122 | # check if field has content | ||
| 123 | if ($sf) { | ||
| 124 | # get subfield codes by pattern | ||
| 125 | # some special characters are allowed as subfiled codes in local defined field | ||
| 126 | # see https://www.loc.gov/marc/96principl.html#eight 8.4.2.3. | ||
| 127 | my @sf_codes = $sf =~ m/\s?\$([a-z0-9!"#\$%&'\(\)\*\+'-\.\/:;<=>])\s/g; | ||
| 128 | |||
| 129 | # split string by subfield code pattern | ||
| 130 | my @sf_values | ||
| 131 | = grep {length $_} | ||
| 132 | split | ||
| 133 | /\s?\$[a-z0-9!"#\$%&'\(\)\*\+'-\.\/:;<=>]\s/, | ||
| 134 | $sf; | ||
| 135 | |||
| 136 | if (scalar @sf_codes != scalar @sf_values) { | ||
| 137 | warn | ||
| 138 | 'different number of subfield codes and values'; | ||
| 139 | next; | ||
| 140 | } | ||
| 141 | |||
| 142 | push @record, | ||
| 143 | [ | ||
| 144 | $tag, $ind1, | ||
| 145 | $ind2, map {$_, shift @sf_values} @sf_codes | ||
| 146 | ]; | ||
| 147 | } | ||
| 148 | |||
| 149 | # skip empty fields | ||
| 150 | else { | ||
| 151 | warn "field $tag has no content"; | ||
| 152 | next; | ||
| 153 | } | ||
| 154 | |||
| 155 | } | ||
| 156 | } | ||
| 157 | return {_id => defined $id ? $id : $count, record => \@record}; | ||
| 158 | } | ||
| 159 | return; | ||
| 160 | 3 | }; | |
| 161 | } | ||
| 162 | |||
| 163 | 2 | 1; | |