35 |
|
|
36 |
=head1 VERSION |
=head1 VERSION |
37 |
|
|
38 |
Version 0.11 |
Version 0.14 |
39 |
|
|
40 |
=cut |
=cut |
41 |
|
|
42 |
our $VERSION = '0.11'; |
our $VERSION = '0.14'; |
43 |
|
|
44 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
45 |
|
|
69 |
row => $row, |
row => $row, |
70 |
rules => $normalize_pl_config, |
rules => $normalize_pl_config, |
71 |
marc_encoding => 'utf-8', |
marc_encoding => 'utf-8', |
72 |
|
config => $config, |
73 |
); |
); |
74 |
|
|
75 |
Options C<lookup>, C<row>, C<rules> and C<log> are mandatory while all |
Options C<lookup>, C<row>, C<rules> and C<log> are mandatory while all |
91 |
no strict 'subs'; |
no strict 'subs'; |
92 |
_set_lookup( $arg->{lookup} ); |
_set_lookup( $arg->{lookup} ); |
93 |
_set_rec( $arg->{row} ); |
_set_rec( $arg->{row} ); |
94 |
|
_set_config( $arg->{config} ); |
95 |
_clean_ds( %{ $arg } ); |
_clean_ds( %{ $arg } ); |
96 |
eval "$arg->{rules}"; |
eval "$arg->{rules}"; |
97 |
die "error evaling $arg->{rules}: $@\n" if ($@); |
die "error evaling $arg->{rules}: $@\n" if ($@); |
113 |
$rec = shift or die "no record hash"; |
$rec = shift or die "no record hash"; |
114 |
} |
} |
115 |
|
|
116 |
|
=head2 _set_config |
117 |
|
|
118 |
|
Set current config hash |
119 |
|
|
120 |
|
_set_config( $config ); |
121 |
|
|
122 |
|
Magic keys are: |
123 |
|
|
124 |
|
=over 4 |
125 |
|
|
126 |
|
=item _ |
127 |
|
|
128 |
|
Code of current database |
129 |
|
|
130 |
|
=item _mfn |
131 |
|
|
132 |
|
Current MFN |
133 |
|
|
134 |
|
=back |
135 |
|
|
136 |
|
=cut |
137 |
|
|
138 |
|
my $config; |
139 |
|
|
140 |
|
sub _set_config { |
141 |
|
$config = shift; |
142 |
|
} |
143 |
|
|
144 |
=head2 _get_ds |
=head2 _get_ds |
145 |
|
|
146 |
Return hash formatted as data structure |
Return hash formatted as data structure |
546 |
warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1); |
warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1); |
547 |
} |
} |
548 |
|
|
549 |
warn "## marc_compose(d) ", dump( $m ),$/ if ($debug > 1); |
warn "## marc_compose current marc = ", dump( $m ),$/ if ($debug > 1); |
550 |
|
|
551 |
push @{ $marc_record->[ $marc_record_offset ] }, $m if ($#{$m} > 2); |
push @{ $marc_record->[ $marc_record_offset ] }, $m if ($#{$m} > 2); |
552 |
} |
} |
679 |
my $f = shift; |
my $f = shift; |
680 |
return unless (defined($rec && $rec->{$f})); |
return unless (defined($rec && $rec->{$f})); |
681 |
my $sf = shift; |
my $sf = shift; |
682 |
return map { $_->{$sf} } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; |
warn "rec2($f,$sf) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
683 |
|
return map { |
684 |
|
if (ref($_->{$sf}) eq 'ARRAY') { |
685 |
|
@{ $_->{$sf} }; |
686 |
|
} else { |
687 |
|
$_->{$sf}; |
688 |
|
} |
689 |
|
} grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; |
690 |
} |
} |
691 |
|
|
692 |
=head2 rec |
=head2 rec |
741 |
=cut |
=cut |
742 |
|
|
743 |
sub prefix { |
sub prefix { |
744 |
my $p = shift or die "prefix needs string as first argument"; |
my $p = shift or return; |
745 |
return map { $p . $_ } grep { defined($_) } @_; |
return map { $p . $_ } grep { defined($_) } @_; |
746 |
} |
} |
747 |
|
|
804 |
} |
} |
805 |
} |
} |
806 |
|
|
807 |
|
=head2 config |
808 |
|
|
809 |
|
Consult config values stored in C<config.yml> |
810 |
|
|
811 |
|
# return database code (key under databases in yaml) |
812 |
|
$database_code = config(); # use _ from hash |
813 |
|
$database_name = config('name'); |
814 |
|
$database_input_name = config('input name'); |
815 |
|
$tag = config('input normalize tag'); |
816 |
|
|
817 |
|
Up to three levels are supported. |
818 |
|
|
819 |
|
=cut |
820 |
|
|
821 |
|
sub config { |
822 |
|
return unless ($config); |
823 |
|
|
824 |
|
my $p = shift; |
825 |
|
|
826 |
|
$p ||= ''; |
827 |
|
|
828 |
|
my $v; |
829 |
|
|
830 |
|
warn "### getting config($p)\n" if ($debug > 1); |
831 |
|
|
832 |
|
my @p = split(/\s+/,$p); |
833 |
|
if ($#p < 0) { |
834 |
|
$v = $config->{ '_' }; # special, database code |
835 |
|
} else { |
836 |
|
|
837 |
|
my $c = dclone( $config ); |
838 |
|
|
839 |
|
foreach my $k (@p) { |
840 |
|
warn "### k: $k c = ",dump($c),$/ if ($debug > 1); |
841 |
|
if (ref($c) eq 'ARRAY') { |
842 |
|
$c = shift @$c; |
843 |
|
warn "config($p) taking first occurence of '$k', probably not what you wanted!\n"; |
844 |
|
last; |
845 |
|
} |
846 |
|
|
847 |
|
if (! defined($c->{$k}) ) { |
848 |
|
$c = undef; |
849 |
|
last; |
850 |
|
} else { |
851 |
|
$c = $c->{$k}; |
852 |
|
} |
853 |
|
} |
854 |
|
$v = $c if ($c); |
855 |
|
|
856 |
|
} |
857 |
|
|
858 |
|
warn "## config( '$p' ) = ",dump( $v ),$/ if ($v && $debug); |
859 |
|
warn "config( '$p' ) is empty\n" if (! $v); |
860 |
|
|
861 |
|
return $v; |
862 |
|
} |
863 |
|
|
864 |
|
=head2 id |
865 |
|
|
866 |
|
Returns unique id of this record |
867 |
|
|
868 |
|
$id = id(); |
869 |
|
|
870 |
|
Returns C<42/2> for 2nd occurence of MFN 42. |
871 |
|
|
872 |
|
=cut |
873 |
|
|
874 |
|
sub id { |
875 |
|
my $mfn = $config->{_mfn} || die "no _mfn in config data"; |
876 |
|
return $mfn . $#{$marc_record} ? $#{$marc_record} + 1 : ''; |
877 |
|
} |
878 |
|
|
879 |
=head2 join_with |
=head2 join_with |
880 |
|
|
881 |
Joins walues with some delimiter |
Joins walues with some delimiter |
886 |
|
|
887 |
sub join_with { |
sub join_with { |
888 |
my $d = shift; |
my $d = shift; |
889 |
|
warn "### join_with('$d',",dump(@_),")\n" if ($debug > 2); |
890 |
my $v = join($d, grep { defined($_) && $_ ne '' } @_); |
my $v = join($d, grep { defined($_) && $_ ne '' } @_); |
891 |
return '' unless defined($v); |
return '' unless defined($v); |
892 |
return $v; |
return $v; |