1 |
package WebPAC::Normalize; |
package WebPAC::Normalize; |
2 |
use Exporter 'import'; |
use Exporter 'import'; |
3 |
@EXPORT = qw/ |
our @EXPORT = qw/ |
4 |
_set_rec _set_lookup |
_set_ds _set_lookup |
5 |
|
get_ds |
6 |
_set_load_row |
_set_load_row |
7 |
_get_ds _clean_ds |
_get_ds _clean_ds |
8 |
_debug |
_debug |
9 |
_pack_subfields_hash |
_pack_subfields_hash |
10 |
|
|
11 |
tag search display |
search_display search display sorted |
12 |
|
|
13 |
marc marc_indicators marc_repeatable_subfield |
marc marc_indicators marc_repeatable_subfield |
14 |
marc_compose marc_leader |
marc_compose marc_leader marc_fixed |
15 |
marc_duplicate marc_remove |
marc_duplicate marc_remove marc_count |
16 |
marc_original_order |
marc_original_order |
17 |
|
|
18 |
rec1 rec2 rec |
rec1 rec2 rec |
19 |
|
frec |
20 |
regex prefix suffix surround |
regex prefix suffix surround |
21 |
first lookup join_with |
first lookup join_with |
22 |
save_into_lookup |
save_into_lookup |
23 |
|
|
24 |
split_rec_on |
split_rec_on |
25 |
|
|
26 |
|
get set |
27 |
|
count |
28 |
|
|
29 |
/; |
/; |
30 |
|
|
31 |
use warnings; |
use warnings; |
39 |
# debugging warn(s) |
# debugging warn(s) |
40 |
my $debug = 0; |
my $debug = 0; |
41 |
|
|
42 |
|
use WebPAC::Normalize::ISBN; |
43 |
|
push @EXPORT, ( 'isbn_10', 'isbn_13' ); |
44 |
|
|
45 |
=head1 NAME |
=head1 NAME |
46 |
|
|
47 |
WebPAC::Normalize - describe normalisaton rules using sets |
WebPAC::Normalize - describe normalisaton rules using sets |
48 |
|
|
|
=head1 VERSION |
|
|
|
|
|
Version 0.22 |
|
|
|
|
49 |
=cut |
=cut |
50 |
|
|
51 |
our $VERSION = '0.22'; |
our $VERSION = '0.32'; |
52 |
|
|
53 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
54 |
|
|
61 |
C<perl -c normalize.pl>. |
C<perl -c normalize.pl>. |
62 |
|
|
63 |
Normalisation can generate multiple output normalized data. For now, supported output |
Normalisation can generate multiple output normalized data. For now, supported output |
64 |
types (on the left side of definition) are: C<tag>, C<display>, C<search> and |
types (on the left side of definition) are: C<search_display>, C<display>, C<search> and |
65 |
C<marc>. |
C<marc>. |
66 |
|
|
67 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
80 |
marc_encoding => 'utf-8', |
marc_encoding => 'utf-8', |
81 |
config => $config, |
config => $config, |
82 |
load_row_coderef => sub { |
load_row_coderef => sub { |
83 |
my ($database,$input,$mfn) = shift; |
my ($database,$input,$mfn) = @_; |
84 |
$store->load_row( database => $database, input => $input, id => $mfn ); |
$store->load_row( database => $database, input => $input, id => $mfn ); |
85 |
}, |
}, |
86 |
); |
); |
108 |
|
|
109 |
no strict 'subs'; |
no strict 'subs'; |
110 |
_set_lookup( $arg->{lookup} ) if defined($arg->{lookup}); |
_set_lookup( $arg->{lookup} ) if defined($arg->{lookup}); |
111 |
_set_rec( $arg->{row} ); |
_set_ds( $arg->{row} ); |
112 |
_set_config( $arg->{config} ) if defined($arg->{config}); |
_set_config( $arg->{config} ) if defined($arg->{config}); |
113 |
_clean_ds( %{ $arg } ); |
_clean_ds( %{ $arg } ); |
114 |
$load_row_coderef = $arg->{load_row_coderef}; |
$load_row_coderef = $arg->{load_row_coderef}; |
119 |
return _get_ds(); |
return _get_ds(); |
120 |
} |
} |
121 |
|
|
122 |
=head2 _set_rec |
=head2 _set_ds |
123 |
|
|
124 |
Set current record hash |
Set current record hash |
125 |
|
|
126 |
_set_rec( $rec ); |
_set_ds( $rec ); |
127 |
|
|
128 |
=cut |
=cut |
129 |
|
|
130 |
my $rec; |
my $rec; |
131 |
|
|
132 |
sub _set_rec { |
sub _set_ds { |
133 |
$rec = shift or die "no record hash"; |
$rec = shift or die "no record hash"; |
134 |
} |
} |
135 |
|
|
136 |
|
=head2 get_ds |
137 |
|
|
138 |
|
Access to original record from input module |
139 |
|
|
140 |
|
my $ds = get_rec; |
141 |
|
|
142 |
|
=cut |
143 |
|
|
144 |
|
sub get_ds { |
145 |
|
return $rec; |
146 |
|
} |
147 |
|
|
148 |
=head2 _set_config |
=head2 _set_config |
149 |
|
|
150 |
Set current config hash |
Set current config hash |
181 |
|
|
182 |
=cut |
=cut |
183 |
|
|
184 |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader); |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader); |
185 |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
186 |
|
|
187 |
sub _get_ds { |
sub _get_ds { |
188 |
|
#warn "## out = ",dump($out); |
189 |
return $out; |
return $out; |
190 |
} |
} |
191 |
|
|
199 |
|
|
200 |
sub _clean_ds { |
sub _clean_ds { |
201 |
my $a = {@_}; |
my $a = {@_}; |
202 |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader) = (); |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader) = (); |
203 |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
204 |
$marc_encoding = $a->{marc_encoding}; |
$marc_encoding = $a->{marc_encoding}; |
205 |
} |
} |
304 |
|
|
305 |
=cut |
=cut |
306 |
|
|
307 |
|
my $fetch_pos; |
308 |
|
|
309 |
sub _get_marc_fields { |
sub _get_marc_fields { |
310 |
|
|
311 |
my $arg = {@_}; |
my $arg = {@_}; |
312 |
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
313 |
my $offset = $marc_fetch_offset; |
$fetch_pos = $marc_fetch_offset; |
314 |
if ($arg->{offset}) { |
if ($arg->{offset}) { |
315 |
$offset = $arg->{offset}; |
$fetch_pos = $arg->{offset}; |
316 |
} elsif($arg->{fetch_next}) { |
} elsif($arg->{fetch_next}) { |
317 |
$marc_fetch_offset++; |
$marc_fetch_offset++; |
318 |
} |
} |
321 |
|
|
322 |
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
323 |
|
|
324 |
my $marc_rec = $marc_record->[ $offset ]; |
my $marc_rec = $marc_record->[ $fetch_pos ]; |
325 |
|
|
326 |
warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
warn "## _get_marc_fields (at offset: $fetch_pos) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
327 |
|
|
328 |
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
329 |
|
|
344 |
|
|
345 |
if ($debug) { |
if ($debug) { |
346 |
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
347 |
warn "## marc_record[$offset] = ", dump( $marc_rec ), $/; |
warn "## marc_record[$fetch_pos] = ", dump( $marc_rec ), $/; |
348 |
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
349 |
warn "## subfield count = ", dump( $u ), $/; |
warn "## subfield count = ", dump( $u ), $/; |
350 |
} |
} |
425 |
return \@m; |
return \@m; |
426 |
} |
} |
427 |
|
|
428 |
|
=head2 _get_marc_leader |
429 |
|
|
430 |
|
Return leader from currently fetched record by L</_get_marc_fields> |
431 |
|
|
432 |
|
print WebPAC::Normalize::_get_marc_leader(); |
433 |
|
|
434 |
|
=cut |
435 |
|
|
436 |
|
sub _get_marc_leader { |
437 |
|
die "no fetch_pos, did you called _get_marc_fields first?" unless ( defined( $fetch_pos ) ); |
438 |
|
return $marc_leader->[ $fetch_pos ]; |
439 |
|
} |
440 |
|
|
441 |
=head2 _debug |
=head2 _debug |
442 |
|
|
443 |
Change level of debug warnings |
Change level of debug warnings |
457 |
|
|
458 |
Those functions generally have to first in your normalization file. |
Those functions generally have to first in your normalization file. |
459 |
|
|
460 |
=head2 tag |
=head2 search_display |
461 |
|
|
462 |
Define new tag for I<search> and I<display>. |
Define output for L<search> and L<display> at the same time |
463 |
|
|
464 |
tag('Title', rec('200','a') ); |
search_display('Title', rec('200','a') ); |
465 |
|
|
466 |
|
|
467 |
=cut |
=cut |
468 |
|
|
469 |
sub tag { |
sub search_display { |
470 |
my $name = shift or die "tag needs name as first argument"; |
my $name = shift or die "search_display needs name as first argument"; |
471 |
my @o = grep { defined($_) && $_ ne '' } @_; |
my @o = grep { defined($_) && $_ ne '' } @_; |
472 |
return unless (@o); |
return unless (@o); |
|
$out->{$name}->{tag} = $name; |
|
473 |
$out->{$name}->{search} = \@o; |
$out->{$name}->{search} = \@o; |
474 |
$out->{$name}->{display} = \@o; |
$out->{$name}->{display} = \@o; |
475 |
} |
} |
476 |
|
|
477 |
|
=head2 tag |
478 |
|
|
479 |
|
Old name for L<search_display>, but supported |
480 |
|
|
481 |
|
=cut |
482 |
|
|
483 |
|
sub tag { |
484 |
|
search_display( @_ ); |
485 |
|
} |
486 |
|
|
487 |
=head2 display |
=head2 display |
488 |
|
|
489 |
Define tag just for I<display> |
Define output just for I<display> |
490 |
|
|
491 |
@v = display('Title', rec('200','a') ); |
@v = display('Title', rec('200','a') ); |
492 |
|
|
493 |
=cut |
=cut |
494 |
|
|
495 |
sub display { |
sub _field { |
496 |
my $name = shift or die "display needs name as first argument"; |
my $type = shift or confess "need type -- BUG?"; |
497 |
|
my $name = shift or confess "needs name as first argument"; |
498 |
my @o = grep { defined($_) && $_ ne '' } @_; |
my @o = grep { defined($_) && $_ ne '' } @_; |
499 |
return unless (@o); |
return unless (@o); |
500 |
$out->{$name}->{tag} = $name; |
$out->{$name}->{$type} = \@o; |
|
$out->{$name}->{display} = \@o; |
|
501 |
} |
} |
502 |
|
|
503 |
|
sub display { _field( 'display', @_ ) } |
504 |
|
|
505 |
=head2 search |
=head2 search |
506 |
|
|
507 |
Prepare values just for I<search> |
Prepare values just for I<search> |
510 |
|
|
511 |
=cut |
=cut |
512 |
|
|
513 |
sub search { |
sub search { _field( 'search', @_ ) } |
514 |
my $name = shift or die "search needs name as first argument"; |
|
515 |
my @o = grep { defined($_) && $_ ne '' } @_; |
=head2 sorted |
516 |
return unless (@o); |
|
517 |
$out->{$name}->{tag} = $name; |
Insert into lists which will be automatically sorted |
518 |
$out->{$name}->{search} = \@o; |
|
519 |
} |
sorted('Title', rec('200','a') ); |
520 |
|
|
521 |
|
=cut |
522 |
|
|
523 |
|
sub sorted { _field( 'sorted', @_ ) } |
524 |
|
|
525 |
|
|
526 |
=head2 marc_leader |
=head2 marc_leader |
527 |
|
|
536 |
my ($offset,$value) = @_; |
my ($offset,$value) = @_; |
537 |
|
|
538 |
if ($offset) { |
if ($offset) { |
539 |
$leader->{ $offset } = $value; |
$marc_leader->[ $marc_record_offset ]->{ $offset } = $value; |
540 |
} else { |
} else { |
541 |
return $leader; |
|
542 |
|
if (defined($marc_leader)) { |
543 |
|
die "marc_leader not array = ", dump( $marc_leader ) unless (ref($marc_leader) eq 'ARRAY'); |
544 |
|
return $marc_leader->[ $marc_record_offset ]; |
545 |
|
} else { |
546 |
|
return; |
547 |
|
} |
548 |
|
} |
549 |
|
} |
550 |
|
|
551 |
|
=head2 marc_fixed |
552 |
|
|
553 |
|
Create control/indentifier fields with values in fixed positions |
554 |
|
|
555 |
|
marc_fixed('008', 00, '070402'); |
556 |
|
marc_fixed('008', 39, '|'); |
557 |
|
|
558 |
|
Positions not specified will be filled with spaces (C<0x20>). |
559 |
|
|
560 |
|
There will be no effort to extend last specified value to full length of |
561 |
|
field in standard. |
562 |
|
|
563 |
|
=cut |
564 |
|
|
565 |
|
sub marc_fixed { |
566 |
|
my ($f, $pos, $val) = @_; |
567 |
|
die "need marc(field, position, value)" unless defined($f) && defined($pos); |
568 |
|
|
569 |
|
confess "need val" unless defined $val; |
570 |
|
|
571 |
|
my $update = 0; |
572 |
|
|
573 |
|
map { |
574 |
|
if ($_->[0] eq $f) { |
575 |
|
my $old = $_->[1]; |
576 |
|
if (length($old) <= $pos) { |
577 |
|
$_->[1] .= ' ' x ( $pos - length($old) ) . $val; |
578 |
|
warn "## marc_fixed($f,$pos,'$val') append '$old' -> '$_->[1]'\n" if ($debug > 1); |
579 |
|
} else { |
580 |
|
$_->[1] = substr($old, 0, $pos) . $val . substr($old, $pos + length($val)); |
581 |
|
warn "## marc_fixed($f,$pos,'$val') update '$old' -> '$_->[1]'\n" if ($debug > 1); |
582 |
|
} |
583 |
|
$update++; |
584 |
|
} |
585 |
|
} @{ $marc_record->[ $marc_record_offset ] }; |
586 |
|
|
587 |
|
if (! $update) { |
588 |
|
my $v = ' ' x $pos . $val; |
589 |
|
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ]; |
590 |
|
warn "## marc_fixed($f,$pos,'val') created '$v'\n" if ($debug > 1); |
591 |
} |
} |
592 |
} |
} |
593 |
|
|
719 |
my $m = $marc_record->[ -1 ]; |
my $m = $marc_record->[ -1 ]; |
720 |
die "can't duplicate record which isn't defined" unless ($m); |
die "can't duplicate record which isn't defined" unless ($m); |
721 |
push @{ $marc_record }, dclone( $m ); |
push @{ $marc_record }, dclone( $m ); |
722 |
warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1); |
push @{ $marc_leader }, dclone( marc_leader() ); |
723 |
|
warn "## marc_duplicate = ", dump(@$marc_leader, @$marc_record), $/ if ($debug > 1); |
724 |
$marc_record_offset = $#{ $marc_record }; |
$marc_record_offset = $#{ $marc_record }; |
725 |
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
726 |
|
|
727 |
} |
} |
728 |
|
|
729 |
=head2 marc_remove |
=head2 marc_remove |
735 |
|
|
736 |
This will erase field C<200> or C<200^a> from current MARC record. |
This will erase field C<200> or C<200^a> from current MARC record. |
737 |
|
|
738 |
|
marc_remove('*'); |
739 |
|
|
740 |
|
Will remove all fields in current MARC record. |
741 |
|
|
742 |
This is useful after calling C<marc_duplicate> or on it's own (but, you |
This is useful after calling C<marc_duplicate> or on it's own (but, you |
743 |
should probably just remove that subfield definition if you are not |
should probably just remove that subfield definition if you are not |
744 |
using C<marc_duplicate>). |
using C<marc_duplicate>). |
756 |
|
|
757 |
warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); |
warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); |
758 |
|
|
759 |
my $i = 0; |
if ($f eq '*') { |
760 |
foreach ( 0 .. $#{ $marc } ) { |
|
761 |
last unless (defined $marc->[$i]); |
delete( $marc_record->[ $marc_record_offset ] ); |
762 |
warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); |
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
763 |
if ($marc->[$i]->[0] eq $f) { |
|
764 |
if (! defined $sf) { |
} else { |
765 |
# remove whole field |
|
766 |
splice @$marc, $i, 1; |
my $i = 0; |
767 |
warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); |
foreach ( 0 .. $#{ $marc } ) { |
768 |
$i--; |
last unless (defined $marc->[$i]); |
769 |
} else { |
warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); |
770 |
foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { |
if ($marc->[$i]->[0] eq $f) { |
771 |
my $o = ($j * 2) + 3; |
if (! defined $sf) { |
772 |
if ($marc->[$i]->[$o] eq $sf) { |
# remove whole field |
773 |
# remove subfield |
splice @$marc, $i, 1; |
774 |
splice @{$marc->[$i]}, $o, 2; |
warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); |
775 |
warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); |
$i--; |
776 |
# is record now empty? |
} else { |
777 |
if ($#{ $marc->[$i] } == 2) { |
foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { |
778 |
splice @$marc, $i, 1; |
my $o = ($j * 2) + 3; |
779 |
warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); |
if ($marc->[$i]->[$o] eq $sf) { |
780 |
$i--; |
# remove subfield |
781 |
}; |
splice @{$marc->[$i]}, $o, 2; |
782 |
|
warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); |
783 |
|
# is record now empty? |
784 |
|
if ($#{ $marc->[$i] } == 2) { |
785 |
|
splice @$marc, $i, 1; |
786 |
|
warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); |
787 |
|
$i--; |
788 |
|
}; |
789 |
|
} |
790 |
} |
} |
791 |
} |
} |
792 |
} |
} |
793 |
|
$i++; |
794 |
} |
} |
|
$i++; |
|
|
} |
|
795 |
|
|
796 |
warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); |
warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); |
797 |
|
|
798 |
$marc_record->[ $marc_record_offset ] = $marc; |
$marc_record->[ $marc_record_offset ] = $marc; |
799 |
|
} |
800 |
|
|
801 |
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
802 |
} |
} |
871 |
warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); |
warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); |
872 |
} |
} |
873 |
|
|
874 |
|
=head2 marc_count |
875 |
|
|
876 |
|
Return number of MARC records created using L</marc_duplicate>. |
877 |
|
|
878 |
|
print "created ", marc_count(), " records"; |
879 |
|
|
880 |
|
=cut |
881 |
|
|
882 |
|
sub marc_count { |
883 |
|
return $#{ $marc_record }; |
884 |
|
} |
885 |
|
|
886 |
|
|
887 |
=head1 Functions to extract data from input |
=head1 Functions to extract data from input |
888 |
|
|
905 |
|
|
906 |
my ($h,$include_subfields) = @_; |
my ($h,$include_subfields) = @_; |
907 |
|
|
908 |
|
# sanity and ease of use |
909 |
|
return $h if (ref($h) ne 'HASH'); |
910 |
|
|
911 |
if ( defined($h->{subfields}) ) { |
if ( defined($h->{subfields}) ) { |
912 |
my $sfs = delete $h->{subfields} || die "no subfields?"; |
my $sfs = delete $h->{subfields} || die "no subfields?"; |
913 |
my @out; |
my @out; |
1007 |
@v = rec('200') |
@v = rec('200') |
1008 |
@v = rec('200','a') |
@v = rec('200','a') |
1009 |
|
|
1010 |
|
If rec() returns just single value, it will |
1011 |
|
return scalar, not array. |
1012 |
|
|
1013 |
=cut |
=cut |
1014 |
|
|
1015 |
|
sub frec { |
1016 |
|
my @out = rec(@_); |
1017 |
|
warn "rec(",dump(@_),") has more than one return value, ignoring\n" if $#out > 0; |
1018 |
|
return shift @out; |
1019 |
|
} |
1020 |
|
|
1021 |
sub rec { |
sub rec { |
1022 |
my @out; |
my @out; |
1023 |
if ($#_ == 0) { |
if ($#_ == 0) { |
1025 |
} elsif ($#_ == 1) { |
} elsif ($#_ == 1) { |
1026 |
@out = rec2(@_); |
@out = rec2(@_); |
1027 |
} |
} |
1028 |
if (@out) { |
if ($#out == 0 && ! wantarray) { |
1029 |
|
return $out[0]; |
1030 |
|
} elsif (@out) { |
1031 |
return @out; |
return @out; |
1032 |
} else { |
} else { |
1033 |
return ''; |
return ''; |
1063 |
=cut |
=cut |
1064 |
|
|
1065 |
sub prefix { |
sub prefix { |
1066 |
my $p = shift or return; |
my $p = shift; |
1067 |
|
return @_ unless defined( $p ); |
1068 |
return map { $p . $_ } grep { defined($_) } @_; |
return map { $p . $_ } grep { defined($_) } @_; |
1069 |
} |
} |
1070 |
|
|
1077 |
=cut |
=cut |
1078 |
|
|
1079 |
sub suffix { |
sub suffix { |
1080 |
my $s = shift or die "suffix needs string as first argument"; |
my $s = shift; |
1081 |
|
return @_ unless defined( $s ); |
1082 |
return map { $_ . $s } grep { defined($_) } @_; |
return map { $_ . $s } grep { defined($_) } @_; |
1083 |
} |
} |
1084 |
|
|
1091 |
=cut |
=cut |
1092 |
|
|
1093 |
sub surround { |
sub surround { |
1094 |
my $p = shift or die "surround need prefix as first argument"; |
my $p = shift; |
1095 |
my $s = shift or die "surround needs suffix as second argument"; |
my $s = shift; |
1096 |
|
$p = '' unless defined( $p ); |
1097 |
|
$s = '' unless defined( $s ); |
1098 |
return map { $p . $_ . $s } grep { defined($_) } @_; |
return map { $p . $_ . $s } grep { defined($_) } @_; |
1099 |
} |
} |
1100 |
|
|
1159 |
sub lookup { |
sub lookup { |
1160 |
my ($what, $database, $input, $key, $having) = @_; |
my ($what, $database, $input, $key, $having) = @_; |
1161 |
|
|
1162 |
confess "lookup needs 5 arguments: what, database, input, key, having" unless ($#_ == 4); |
confess "lookup needs 5 arguments: what, database, input, key, having\n" unless ($#_ == 4); |
1163 |
|
|
1164 |
warn "## lookup ($database, $input, $key)", $/ if ($debug > 1); |
warn "## lookup ($database, $input, $key)", $/ if ($debug > 1); |
1165 |
return unless (defined($lookup->{$database}->{$input}->{$key})); |
return unless (defined($lookup->{$database}->{$input}->{$key})); |
1173 |
|
|
1174 |
foreach my $h ( @having ) { |
foreach my $h ( @having ) { |
1175 |
if (defined($lookup->{$database}->{$input}->{$key}->{$h})) { |
if (defined($lookup->{$database}->{$input}->{$key}->{$h})) { |
1176 |
warn "lookup for $database/$input/$key/$h return ",dump($lookup->{$database}->{$input}->{$key}->{$h}),"\n"; |
warn "lookup for $database/$input/$key/$h return ",dump($lookup->{$database}->{$input}->{$key}->{$h}),"\n" if ($debug); |
1177 |
$mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} }; |
$mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} }; |
1178 |
} |
} |
1179 |
} |
} |
1182 |
|
|
1183 |
my @mfns = sort keys %$mfns; |
my @mfns = sort keys %$mfns; |
1184 |
|
|
1185 |
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); |
1186 |
|
|
1187 |
my $old_rec = $rec; |
my $old_rec = $rec; |
1188 |
my @out; |
my @out; |
1190 |
foreach my $mfn (@mfns) { |
foreach my $mfn (@mfns) { |
1191 |
$rec = $load_row_coderef->( $database, $input, $mfn ); |
$rec = $load_row_coderef->( $database, $input, $mfn ); |
1192 |
|
|
1193 |
warn "got $database/$input/$mfn = ", dump($rec), $/; |
warn "got $database/$input/$mfn = ", dump($rec), $/ if ($debug); |
1194 |
|
|
1195 |
my @vals = $what->(); |
my @vals = $what->(); |
1196 |
|
|
1197 |
push @out, ( @vals ); |
push @out, ( @vals ); |
1198 |
|
|
1199 |
warn "lookup for mfn $mfn returned ", dump(@vals), $/; |
warn "lookup for mfn $mfn returned ", dump(@vals), $/ if ($debug); |
1200 |
} |
} |
1201 |
|
|
1202 |
# if (ref($lookup->{$k}) eq 'ARRAY') { |
# if (ref($lookup->{$k}) eq 'ARRAY') { |
1207 |
|
|
1208 |
$rec = $old_rec; |
$rec = $old_rec; |
1209 |
|
|
1210 |
warn "## lookup returns = ", dump(@out), $/; |
warn "## lookup returns = ", dump(@out), $/ if ($debug); |
1211 |
|
|
1212 |
if ($#out == 0) { |
if ($#out == 0) { |
1213 |
return $out[0]; |
return $out[0]; |
1269 |
$database_code = config(); # use _ from hash |
$database_code = config(); # use _ from hash |
1270 |
$database_name = config('name'); |
$database_name = config('name'); |
1271 |
$database_input_name = config('input name'); |
$database_input_name = config('input name'); |
|
$tag = config('input normalize tag'); |
|
1272 |
|
|
1273 |
Up to three levels are supported. |
Up to three levels are supported. |
1274 |
|
|
1384 |
} |
} |
1385 |
} |
} |
1386 |
|
|
1387 |
|
my $hash; |
1388 |
|
|
1389 |
|
=head2 set |
1390 |
|
|
1391 |
|
set( key => 'value' ); |
1392 |
|
|
1393 |
|
=cut |
1394 |
|
|
1395 |
|
sub set { |
1396 |
|
my ($k,$v) = @_; |
1397 |
|
warn "## set ( $k => ", dump($v), " )", $/ if ( $debug ); |
1398 |
|
$hash->{$k} = $v; |
1399 |
|
}; |
1400 |
|
|
1401 |
|
=head2 get |
1402 |
|
|
1403 |
|
get( 'key' ); |
1404 |
|
|
1405 |
|
=cut |
1406 |
|
|
1407 |
|
sub get { |
1408 |
|
my $k = shift || return; |
1409 |
|
my $v = $hash->{$k}; |
1410 |
|
warn "## get $k = ", dump( $v ), $/ if ( $debug ); |
1411 |
|
return $v; |
1412 |
|
} |
1413 |
|
|
1414 |
|
=head2 count |
1415 |
|
|
1416 |
|
if ( count( @result ) == 1 ) { |
1417 |
|
# do something if only 1 result is there |
1418 |
|
} |
1419 |
|
|
1420 |
|
=cut |
1421 |
|
|
1422 |
|
sub count { |
1423 |
|
warn "## count ",dump(@_),$/ if ( $debug ); |
1424 |
|
return @_ . ''; |
1425 |
|
} |
1426 |
|
|
1427 |
# END |
# END |
1428 |
1; |
1; |