--- trunk/lib/WebPAC/Normalize.pm 2006/12/10 12:45:11 786 +++ trunk/lib/WebPAC/Normalize.pm 2007/04/05 21:50:14 817 @@ -9,8 +9,8 @@ tag search display marc marc_indicators marc_repeatable_subfield - marc_compose marc_leader - marc_duplicate marc_remove + marc_compose marc_leader marc_fixed + marc_duplicate marc_remove marc_count marc_original_order rec1 rec2 rec @@ -21,6 +21,7 @@ split_rec_on get set + count /; use warnings; @@ -41,11 +42,11 @@ =head1 VERSION -Version 0.25 +Version 0.28 =cut -our $VERSION = '0.25'; +our $VERSION = '0.28'; =head1 SYNOPSIS @@ -166,7 +167,7 @@ =cut -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); my ($marc_record_offset, $marc_fetch_offset) = (0, 0); sub _get_ds { @@ -183,7 +184,7 @@ sub _clean_ds { my $a = {@_}; - ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader) = (); + ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader) = (); ($marc_record_offset, $marc_fetch_offset) = (0,0); $marc_encoding = $a->{marc_encoding}; } @@ -288,13 +289,15 @@ =cut +my $fetch_pos; + sub _get_marc_fields { my $arg = {@_}; warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); - my $offset = $marc_fetch_offset; + $fetch_pos = $marc_fetch_offset; if ($arg->{offset}) { - $offset = $arg->{offset}; + $fetch_pos = $arg->{offset}; } elsif($arg->{fetch_next}) { $marc_fetch_offset++; } @@ -303,9 +306,9 @@ warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); - my $marc_rec = $marc_record->[ $offset ]; + my $marc_rec = $marc_record->[ $fetch_pos ]; - 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); return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); @@ -326,7 +329,7 @@ if ($debug) { warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); - warn "## marc_record[$offset] = ", dump( $marc_rec ), $/; + warn "## marc_record[$fetch_pos] = ", dump( $marc_rec ), $/; warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; warn "## subfield count = ", dump( $u ), $/; } @@ -407,6 +410,19 @@ return \@m; } +=head2 _get_marc_leader + +Return leader from currently fetched record by L + + print WebPAC::Normalize::_get_marc_leader(); + +=cut + +sub _get_marc_leader { + die "no fetch_pos, did you called _get_marc_fields first?" unless ( defined( $fetch_pos ) ); + return $marc_leader->[ $fetch_pos ]; +} + =head2 _debug Change level of debug warnings @@ -489,9 +505,56 @@ my ($offset,$value) = @_; if ($offset) { - $leader->{ $offset } = $value; + $marc_leader->[ $marc_record_offset ]->{ $offset } = $value; } else { - return $leader; + + if (defined($marc_leader)) { + die "marc_leader not array = ", dump( $marc_leader ) unless (ref($marc_leader) eq 'ARRAY'); + return $marc_leader->[ $marc_record_offset ]; + } else { + return; + } + } +} + +=head2 marc_fixed + +Create control/indentifier fields with values in fixed positions + + marc_fixed('008', 00, '070402'); + marc_fixed('008', 39, '|'); + +Positions not specified will be filled with spaces (C<0x20>). + +There will be no effort to extend last specified value to full length of +field in standard. + +=cut + +sub marc_fixed { + my ($f, $pos, $val) = @_; + die "need marc(field, position, value)" unless defined($f) && defined($pos); + + my $update = 0; + + map { + if ($_->[0] eq $f) { + my $old = $_->[1]; + if (length($old) < $pos) { + $_->[1] .= ' ' x ( $pos - length($old) ) . $val; + warn "## marc_fixed($f,$pos,'$val') append '$old' -> '$_->[1]'\n" if ($debug > 1); + } else { + $_->[1] = substr($old, 0, $pos) . $val . substr($old, $pos + length($val)); + warn "## marc_fixed($f,$pos,'$val') update '$old' -> '$_->[1]'\n" if ($debug > 1); + } + $update++; + } + } @{ $marc_record->[ $marc_record_offset ] }; + + if (! $update) { + my $v = ' ' x $pos . $val; + push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ]; + warn "## marc_fixed($f,$pos,'val') created '$v'\n" if ($debug > 1); } } @@ -623,9 +686,11 @@ my $m = $marc_record->[ -1 ]; die "can't duplicate record which isn't defined" unless ($m); push @{ $marc_record }, dclone( $m ); - warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1); + push @{ $marc_leader }, dclone( marc_leader() ); + warn "## marc_duplicate = ", dump(@$marc_leader, @$marc_record), $/ if ($debug > 1); $marc_record_offset = $#{ $marc_record }; warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); + } =head2 marc_remove @@ -661,6 +726,7 @@ if ($f eq '*') { delete( $marc_record->[ $marc_record_offset ] ); + warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); } else { @@ -699,7 +765,6 @@ $marc_record->[ $marc_record_offset ] = $marc; } - warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); } @@ -773,6 +838,18 @@ warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); } +=head2 marc_count + +Return number of MARC records created using L. + + print "created ", marc_count(), " records"; + +=cut + +sub marc_count { + return $#{ $marc_record }; +} + =head1 Functions to extract data from input @@ -1272,7 +1349,7 @@ sub set { my ($k,$v) = @_; - warn "## set ( $k => ", dump($v), " )", $/; + warn "## set ( $k => ", dump($v), " )", $/ if ( $debug ); $hash->{$k} = $v; }; @@ -1285,10 +1362,22 @@ sub get { my $k = shift || return; my $v = $hash->{$k}; - warn "## get $k = ", dump( $v ), $/; + warn "## get $k = ", dump( $v ), $/ if ( $debug ); return $v; } +=head2 count + + if ( count( @result ) == 1 ) { + # do something if only 1 result is there + } + +=cut + +sub count { + warn "## count ",dump(@_),$/ if ( $debug ); + return @_ . ''; +} # END 1;