--- trunk/lib/WebPAC/Normalize.pm 2006/06/26 16:39:51 536 +++ trunk/lib/WebPAC/Normalize.pm 2006/06/29 15:29:41 540 @@ -1,9 +1,12 @@ package WebPAC::Normalize; use Exporter 'import'; @EXPORT = qw/ - set_rec set_lookup - get_ds clean_ds + _set_rec _set_lookup + _get_ds _clean_ds + tag search display + marc21 + rec1 rec2 rec regex prefix suffix surround first lookup join_with @@ -21,11 +24,11 @@ =head1 VERSION -Version 0.04 +Version 0.05 =cut -our $VERSION = '0.04'; +our $VERSION = '0.05'; =head1 SYNOPSIS @@ -38,22 +41,32 @@ C. Normalisation can generate multiple output normalized data. For now, supported output -types (on the left side of definition) are: C, C and C. +types (on the left side of definition) are: C, C, C and +C. =head1 FUNCTIONS +Functions which start with C<_> are private and used by WebPAC internally. +All other functions are available for use within normalisation rules. + =head2 data_structure Return data structure - my $ds = WebPAC::Normalize( + my $ds = WebPAC::Normalize::data_structure( lookup => $lookup->lookup_hash, row => $row, rules => $normalize_pl_config, ); +Options C, C, C and C are mandatory while all +other are optional. + This function will B if normalizastion can't be evaled. +Since this function isn't exported you have to call it with +C. + =cut sub data_structure { @@ -63,28 +76,88 @@ die "need normalisation argument" unless ($arg->{rules}); no strict 'subs'; - set_lookup( $arg->{lookup} ); - set_rec( $arg->{row} ); - clean_ds(); + _set_lookup( $arg->{lookup} ); + _set_rec( $arg->{row} ); + _clean_ds(); + eval "$arg->{rules}"; die "error evaling $arg->{rules}: $@\n" if ($@); - return get_ds(); + + return _get_ds(); } -=head2 set_rec +=head2 _set_rec Set current record hash - set_rec( $rec ); + _set_rec( $rec ); =cut my $rec; -sub set_rec { +sub _set_rec { $rec = shift or die "no record hash"; } +=head2 _get_ds + +Return hash formatted as data structure + + my $ds = _get_ds(); + +=cut + +my $out; +my $marc21; + +sub _get_ds { + return $out; +} + +=head2 _clean_ds + +Clean data structure hash for next record + + _clean_ds(); + +=cut + +sub _clean_ds { + $out = undef; + $marc21 = undef; +} + +=head2 _set_lookup + +Set current lookup hash + + _set_lookup( $lookup ); + +=cut + +my $lookup; + +sub _set_lookup { + $lookup = shift; +} + +=head2 _get_marc21_fields + +Get all fields defined by calls to C + + $marc->add_fields( WebPAC::Normalize:_get_marc21_fields() ); + +=cut + +sub _get_marc21_fields { + return @{$marc21}; +} + +=head1 Functions to create C + +Those functions generally have to first in your normalization file. + =head2 tag Define new tag for I and I. @@ -94,8 +167,6 @@ =cut -my $out; - sub tag { my $name = shift or die "tag needs name as first argument"; my @o = grep { defined($_) && $_ ne '' } @_; @@ -137,43 +208,29 @@ $out->{$name}->{search} = \@o; } -=head2 get_ds +=head2 marc21 -Return hash formatted as data structure +Save value for MARC field - my $ds = get_ds(); + marc21('900','a', rec('200','a') ); =cut -sub get_ds { - return $out; -} - -=head2 clean_ds - -Clean data structure hash for next record +sub marc21 { + my $f = shift or die "marc21 needs field"; + die "marc21 field must be numer" unless ($f =~ /^\d+$/); - clean_ds(); + my $sf = shift or die "marc21 needs subfield"; -=cut - -sub clean_ds { - $out = undef; + foreach my $v (@_) { + push @{ $marc21 }, [ $f, ' ', ' ', $sf => $v ]; + } } -=head2 set_lookup - -Set current lookup hash - - set_lookup( $lookup ); - -=cut +=head1 Functions to extract data from input -my $lookup; - -sub set_lookup { - $lookup = shift; -} +This function should be used inside functions to create C described +above. =head2 rec1