/usr/share/perl5/Catmandu/Fix/Inline/marc_map.pm is in libcatmandu-marc-perl 0.206-2.
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 | =head1 NAME
Catmandu::Fix::Inline::marc_map - A marc_map-er for Perl scripts
=head1 SYNOPSIS
use Catmandu::Fix::Inline::marc_map qw(:all);
my $title = marc_map($data,'245a');
my @authors = marc_map($data,'100ab');
# Get all 245 in an array
@arr = marc_map($data,'245');
# Or as a string
$str = marc_map($data,'245');
# str joined by a semi-colon
$f245 = marc_map($data, '245', -join , ';');
# Get the 245-$a$b$c subfields ordered as given in the record
$str = marc_map($data,'245abc');
# Get the 245-$c$b$a subfields orders as given in the mapping
$str = marc_map($data,'245cba', -pluck => 1);
# Get the 008 characters 35-35
$str = marc_map($data,'008_/35-35');
# Get all 100 subfields except the digits
$str = marc_map($data,'100^0123456789');
# The $data should be a Catmandu-style MARC hash
{ record => [
['field', 'ind1' , 'ind2' , 'subfieldcode or underscore' , 'data' , 'subfield' , 'data' , ...] ,
...
]};
# Example
$data = { record => [
['001' , ' ', ' ' , '_' , 'myrecord-001' ] ,
['020' , ' ', ' ' , 'a' , '978-1449303587' ] ,
['245' , ' ', ' ' , 'a' , 'Learning Per' , 'c', '/ by Randal L. Schwartz'],
]};
=head1 SEE ALSO
L<Catmandu::Fix::Inline::marc_add> , L<Catmandu::Fix::Inline::marc_remove>
=cut
package Catmandu::Fix::Inline::marc_map;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(marc_map);
%EXPORT_TAGS = (all => [qw(marc_map)]);
sub marc_map {
my ($data,$marc_path,%opts) = @_;
return unless exists $data->{'record'};
my $record = $data->{'record'};
unless (defined $record && ref $record eq 'ARRAY') {
return wantarray ? () : undef;
}
my $split = $opts{'-split'};
my $join_char = $opts{'-join'} // '';
my $pluck = $opts{'-pluck'};
my $attrs = {};
if ($marc_path =~ /(\S{3})(\[(.)?,?(.)?\])?([_a-z0-9^]+)?(\/(\d+)(-(\d+))?)?/) {
$attrs->{field} = $1;
$attrs->{ind1} = $3;
$attrs->{ind2} = $4;
$attrs->{subfield_regex} = defined $5 ? "[$5]" : "[a-z0-9_]";
$attrs->{from} = $7;
$attrs->{to} = $9;
} else {
return wantarray ? () : undef;
}
$attrs->{field_regex} = $attrs->{field};
$attrs->{field_regex} =~ s/\*/./g;
my $add_subfields = sub {
my $var = shift;
my $start = shift;
my @v = ();
if ($pluck) {
# Treat the subfield_regex as a hash index
my $_h = {};
for (my $i = $start; $i < @$var; $i += 2) {
push @{ $_h->{ $var->[$i] } } , $var->[$i + 1];
}
for my $c (split('',$attrs->{subfield_regex})) {
push @v , @{ $_h->{$c} } if exists $_h->{$c};
}
}
else {
for (my $i = $start; $i < @$var; $i += 2) {
if ($var->[$i] =~ /$attrs->{subfield_regex}/) {
push(@v, $var->[$i + 1]);
}
}
}
return \@v;
};
my @vals = ();
for my $var (@$record) {
next if $var->[0] !~ /$attrs->{field_regex}/;
next if defined $attrs->{ind1} && $var->[1] ne $attrs->{ind1};
next if defined $attrs->{ind2} && $var->[2] ne $attrs->{ind2};
my $v;
if ($var->[0] =~ /LDR|00./) {
$v = $add_subfields->($var,3);
}
elsif (defined $var->[5] && $var->[5] eq '_') {
$v = $add_subfields->($var,5);
}
else {
$v = $add_subfields->($var,3);
}
if (@$v) {
if (!$split) {
$v = join $join_char, @$v;
if (defined(my $off = $attrs->{from})) {
my $len = defined $attrs->{to} ? $attrs->{to} - $off + 1 : 1;
$v = substr($v,$off,$len);
}
}
}
push (@vals,$v) if ( (ref $v eq 'ARRAY' && @$v) || (ref $v eq '' && length $v ));
}
if (wantarray) {
return @vals;
}
elsif (@vals > 0) {
return join $join_char , @vals;
}
else {
return undef;
}
}
1;
|