2 |
use Exporter 'import'; |
use Exporter 'import'; |
3 |
@EXPORT = qw/ |
@EXPORT = qw/ |
4 |
_set_rec _set_lookup |
_set_rec _set_lookup |
5 |
_set_load_ds |
_set_load_row |
6 |
_get_ds _clean_ds |
_get_ds _clean_ds |
7 |
_debug |
_debug |
8 |
_pack_subfields_hash |
_pack_subfields_hash |
39 |
|
|
40 |
=head1 VERSION |
=head1 VERSION |
41 |
|
|
42 |
Version 0.21 |
Version 0.23 |
43 |
|
|
44 |
=cut |
=cut |
45 |
|
|
46 |
our $VERSION = '0.21'; |
our $VERSION = '0.23'; |
47 |
|
|
48 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
49 |
|
|
74 |
rules => $normalize_pl_config, |
rules => $normalize_pl_config, |
75 |
marc_encoding => 'utf-8', |
marc_encoding => 'utf-8', |
76 |
config => $config, |
config => $config, |
77 |
load_ds_coderef => sub { |
load_row_coderef => sub { |
78 |
my ($database,$input,$mfn) = shift; |
my ($database,$input,$mfn) = shift; |
79 |
$store->load_ds( database => $database, input => $input, id => $mfn ); |
$store->load_row( database => $database, input => $input, id => $mfn ); |
80 |
}, |
}, |
81 |
); |
); |
82 |
|
|
83 |
Options C<row>, C<rules> and C<log> are mandatory while all |
Options C<row>, C<rules> and C<log> are mandatory while all |
84 |
other are optional. |
other are optional. |
85 |
|
|
86 |
C<load_ds_coderef> is closure only used when executing lookups, so they will |
C<load_row_coderef> is closure only used when executing lookups, so they will |
87 |
die if it's not defined. |
die if it's not defined. |
88 |
|
|
89 |
This function will B<die> if normalizastion can't be evaled. |
This function will B<die> if normalizastion can't be evaled. |
93 |
|
|
94 |
=cut |
=cut |
95 |
|
|
96 |
my $load_ds_coderef; |
my $load_row_coderef; |
97 |
|
|
98 |
sub data_structure { |
sub data_structure { |
99 |
my $arg = {@_}; |
my $arg = {@_}; |
106 |
_set_rec( $arg->{row} ); |
_set_rec( $arg->{row} ); |
107 |
_set_config( $arg->{config} ) if defined($arg->{config}); |
_set_config( $arg->{config} ) if defined($arg->{config}); |
108 |
_clean_ds( %{ $arg } ); |
_clean_ds( %{ $arg } ); |
109 |
$load_ds_coderef = $arg->{load_ds_coderef}; |
$load_row_coderef = $arg->{load_row_coderef}; |
110 |
|
|
111 |
eval "$arg->{rules}"; |
eval "$arg->{rules}"; |
112 |
die "error evaling $arg->{rules}: $@\n" if ($@); |
die "error evaling $arg->{rules}: $@\n" if ($@); |
164 |
|
|
165 |
=cut |
=cut |
166 |
|
|
167 |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader); |
168 |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
169 |
|
|
170 |
sub _get_ds { |
sub _get_ds { |
181 |
|
|
182 |
sub _clean_ds { |
sub _clean_ds { |
183 |
my $a = {@_}; |
my $a = {@_}; |
184 |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (); |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader) = (); |
185 |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
186 |
$marc_encoding = $a->{marc_encoding}; |
$marc_encoding = $a->{marc_encoding}; |
187 |
} |
} |
212 |
return $lookup; |
return $lookup; |
213 |
} |
} |
214 |
|
|
215 |
=head2 _set_load_ds |
=head2 _set_load_row |
216 |
|
|
217 |
Setup code reference which will return L<data_structure> from |
Setup code reference which will return L<data_structure> from |
218 |
L<WebPAC::Store> |
L<WebPAC::Store> |
219 |
|
|
220 |
_set_load_ds(sub { |
_set_load_row(sub { |
221 |
my ($database,$input,$mfn) = @_; |
my ($database,$input,$mfn) = @_; |
222 |
$store->load_ds( database => $database, input => $input, id => $mfn ); |
$store->load_row( database => $database, input => $input, id => $mfn ); |
223 |
}); |
}); |
224 |
|
|
225 |
=cut |
=cut |
226 |
|
|
227 |
sub _set_load_ds { |
sub _set_load_row { |
228 |
my $coderef = shift; |
my $coderef = shift; |
229 |
confess "argument isn't CODE" unless ref($coderef) eq 'CODE'; |
confess "argument isn't CODE" unless ref($coderef) eq 'CODE'; |
230 |
|
|
231 |
$load_ds_coderef = $coderef; |
$load_row_coderef = $coderef; |
232 |
} |
} |
233 |
|
|
234 |
=head2 _get_marc_fields |
=head2 _get_marc_fields |
487 |
my ($offset,$value) = @_; |
my ($offset,$value) = @_; |
488 |
|
|
489 |
if ($offset) { |
if ($offset) { |
490 |
$out->{' leader'}->{ $offset } = $value; |
$leader->{ $offset } = $value; |
491 |
} else { |
} else { |
492 |
return $out->{' leader'}; |
return $leader; |
493 |
} |
} |
494 |
} |
} |
495 |
|
|
880 |
@v = rec('200') |
@v = rec('200') |
881 |
@v = rec('200','a') |
@v = rec('200','a') |
882 |
|
|
883 |
|
If rec() returns just single value, it will |
884 |
|
return scalar, not array. |
885 |
|
|
886 |
=cut |
=cut |
887 |
|
|
888 |
sub rec { |
sub rec { |
892 |
} elsif ($#_ == 1) { |
} elsif ($#_ == 1) { |
893 |
@out = rec2(@_); |
@out = rec2(@_); |
894 |
} |
} |
895 |
if (@out) { |
if ($#out == 0 && ! wantarray) { |
896 |
|
return $out[0]; |
897 |
|
} elsif (@out) { |
898 |
return @out; |
return @out; |
899 |
} else { |
} else { |
900 |
return ''; |
return ''; |
1027 |
warn "## lookup ($database, $input, $key)", $/ if ($debug > 1); |
warn "## lookup ($database, $input, $key)", $/ if ($debug > 1); |
1028 |
return unless (defined($lookup->{$database}->{$input}->{$key})); |
return unless (defined($lookup->{$database}->{$input}->{$key})); |
1029 |
|
|
1030 |
confess "lookup really need load_ds_coderef added to data_structure\n" unless ($load_ds_coderef); |
confess "lookup really need load_row_coderef added to data_structure\n" unless ($load_row_coderef); |
1031 |
|
|
1032 |
my $mfns; |
my $mfns; |
1033 |
my @having = $having->(); |
my @having = $having->(); |
1045 |
|
|
1046 |
my @mfns = sort keys %$mfns; |
my @mfns = sort keys %$mfns; |
1047 |
|
|
1048 |
warn "# lookup loading $database/$input/$key mfn ", join(",",@mfns)," having ",dump(@having),"\n"; |
warn "# lookup loading $database/$input/$key mfn ", join(",",@mfns)," having ",dump(@having),"\n" if ($debug); |
1049 |
|
|
1050 |
my $old_rec = $rec; |
my $old_rec = $rec; |
1051 |
my @out; |
my @out; |
1052 |
|
|
1053 |
foreach my $mfn (@mfns) { |
foreach my $mfn (@mfns) { |
1054 |
$rec = $load_ds_coderef->( $database, $input, $mfn ); |
$rec = $load_row_coderef->( $database, $input, $mfn ); |
1055 |
|
|
1056 |
warn "got $database/$input/$mfn = ", dump($rec), $/; |
warn "got $database/$input/$mfn = ", dump($rec), $/; |
1057 |
|
|
1070 |
|
|
1071 |
$rec = $old_rec; |
$rec = $old_rec; |
1072 |
|
|
1073 |
warn "## lookup returns = ", dump(@out), $/; |
warn "## lookup returns = ", dump(@out), $/ if ($debug); |
1074 |
|
|
1075 |
return @out; |
if ($#out == 0) { |
1076 |
|
return $out[0]; |
1077 |
|
} else { |
1078 |
|
return @out; |
1079 |
|
} |
1080 |
} |
} |
1081 |
|
|
1082 |
=head2 save_into_lookup |
=head2 save_into_lookup |