/usr/share/perl5/Catmandu/Fix/Inline/marc_set.pm is in libcatmandu-marc-perl 0.214-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 | package Catmandu::Fix::Inline::marc_set;
use Clone qw(clone);
use Carp;
use Catmandu::Util qw(:is);
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(marc_set);
%EXPORT_TAGS = (all => [qw(marc_set)]);
sub marc_set {
my ($data,$marc_path,$value) = @_;
my $record = $data->{record};
return $data unless defined $record;
if ($value =~ /^\$\.(\S+)/) {
my $path = $1;
$value = Catmandu::Util::data_at($path,$data);
}
if (is_array_ref $value) {
$value = $value->[-1];
}
elsif (is_hash_ref $value) {
my $last;
for (keys %$value) {
$last = $value->{$_};
}
$value = $last;
}
my $field_regex;
my ($field,$ind1,$ind2,$subfield_regex,$from,$to,$len);
if ($marc_path =~ /(\S{3})(\[(.)?,?(.)?\])?([_a-z0-9])?(\/(\d+)(-(\d+))?)?/) {
$field = $1;
$ind1 = $3;
$ind2 = $4;
if (defined $5) {
$subfield_regex = "$5";
}
else {
$subfield_regex = ($field =~ /^LDR|^00/) ? "_" : "a";
}
$from = $7;
$to = $9;
$len = defined $to ? $to - $from + 1 : 1;
}
else {
confess "invalid marc path";
}
$field_regex = $field;
$field_regex =~ s/\*/./g;
for (@$record) {
if ($_->[0] !~ /$field_regex/) {
next;
}
if (defined $ind1) {
if (!defined $_->[1] || $_->[1] ne $ind1) {
next;
}
}
if (defined $ind2) {
if (!defined $_->[2] || $_->[2] ne $ind2) {
next;
}
}
my $start;
if ($_->[0] =~ /^LDR|^00/) {
$start = 3;
}
elsif (defined $_->[5] && $_->[5] eq '_') {
$start = 5;
}
else {
$start = 3;
}
my $found = 0;
for (my $i = $start; $i < @$_; $i += 2) {
if ($_->[$i] eq $subfield_regex) {
if (defined $from) {
substr($_->[$i + 1], $from, $len) = $value;
}
else {
$_->[$i + 1] = $value;
}
$found = 1;
}
}
if ($found == 0) {
push(@$_,$subfield_regex,$value);
}
}
$data;
}
=head1 NAME
Catmandu::Fix::Inline::marc_set - A marc_set-er for Perl scripts
=head1 SYNOPSIS
use Catmandu::Fix::Inline::marc_set qw(:all);
# Set to literal value
my $data = marc_set($data,'245[1]a', 'value');
# Set to a copy of a deeply nested JSON path
my $data = marc_set($data,'245[1]a', '$.my.deep.field');
=head1 SEE ALSO
L<Catmandu::Fix::Inline::marc_add> ,
L<Catmandu::Fix::Inline::marc_remove> ,
L<Catmandu::Fix::Inline::marc_map>
=cut
1;
|