/[webpac2]/trunk/lib/WebPAC/Normalize.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Normalize.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 740 by dpavlin, Sat Oct 7 16:33:37 2006 UTC revision 786 by dpavlin, Sun Dec 10 12:45:11 2006 UTC
# Line 19  use Exporter 'import'; Line 19  use Exporter 'import';
19          save_into_lookup          save_into_lookup
20    
21          split_rec_on          split_rec_on
22    
23            get set
24  /;  /;
25    
26  use warnings;  use warnings;
# Line 39  WebPAC::Normalize - describe normalisato Line 41  WebPAC::Normalize - describe normalisato
41    
42  =head1 VERSION  =head1 VERSION
43    
44  Version 0.22  Version 0.25
45    
46  =cut  =cut
47    
48  our $VERSION = '0.22';  our $VERSION = '0.25';
49    
50  =head1 SYNOPSIS  =head1 SYNOPSIS
51    
# Line 164  Return hash formatted as data structure Line 166  Return hash formatted as data structure
166    
167  =cut  =cut
168    
169  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators);  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader);
170  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);
171    
172  sub _get_ds {  sub _get_ds {
# Line 181  Clean data structure hash for next recor Line 183  Clean data structure hash for next recor
183    
184  sub _clean_ds {  sub _clean_ds {
185          my $a = {@_};          my $a = {@_};
186          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = ();          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader) = ();
187          ($marc_record_offset, $marc_fetch_offset) = (0,0);          ($marc_record_offset, $marc_fetch_offset) = (0,0);
188          $marc_encoding = $a->{marc_encoding};          $marc_encoding = $a->{marc_encoding};
189  }  }
# Line 487  sub marc_leader { Line 489  sub marc_leader {
489          my ($offset,$value) = @_;          my ($offset,$value) = @_;
490    
491          if ($offset) {          if ($offset) {
492                  $out->{' leader'}->{ $offset } = $value;                  $leader->{ $offset } = $value;
493          } else {          } else {
494                  return $out->{' leader'};                  return $leader;
495          }          }
496  }  }
497    
# Line 635  Remove some field or subfield from MARC Line 637  Remove some field or subfield from MARC
637    
638  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.
639    
640      marc_remove('*');
641    
642    Will remove all fields in current MARC record.
643    
644  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
645  should probably just remove that subfield definition if you are not  should probably just remove that subfield definition if you are not
646  using C<marc_duplicate>).  using C<marc_duplicate>).
# Line 652  sub marc_remove { Line 658  sub marc_remove {
658    
659          warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2);          warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2);
660    
661          my $i = 0;          if ($f eq '*') {
662          foreach ( 0 .. $#{ $marc } ) {  
663                  last unless (defined $marc->[$i]);                  delete( $marc_record->[ $marc_record_offset ] );
664                  warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3);  
665                  if ($marc->[$i]->[0] eq $f) {          } else {
666                          if (! defined $sf) {  
667                                  # remove whole field                  my $i = 0;
668                                  splice @$marc, $i, 1;                  foreach ( 0 .. $#{ $marc } ) {
669                                  warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3);                          last unless (defined $marc->[$i]);
670                                  $i--;                          warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3);
671                          } else {                          if ($marc->[$i]->[0] eq $f) {
672                                  foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) {                                  if (! defined $sf) {
673                                          my $o = ($j * 2) + 3;                                          # remove whole field
674                                          if ($marc->[$i]->[$o] eq $sf) {                                          splice @$marc, $i, 1;
675                                                  # remove subfield                                          warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3);
676                                                  splice @{$marc->[$i]}, $o, 2;                                          $i--;
677                                                  warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3);                                  } else {
678                                                  # is record now empty?                                          foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) {
679                                                  if ($#{ $marc->[$i] } == 2) {                                                  my $o = ($j * 2) + 3;
680                                                          splice @$marc, $i, 1;                                                  if ($marc->[$i]->[$o] eq $sf) {
681                                                          warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3);                                                          # remove subfield
682                                                          $i--;                                                          splice @{$marc->[$i]}, $o, 2;
683                                                  };                                                          warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3);
684                                                            # is record now empty?
685                                                            if ($#{ $marc->[$i] } == 2) {
686                                                                    splice @$marc, $i, 1;
687                                                                    warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3);
688                                                                    $i--;
689                                                            };
690                                                    }
691                                          }                                          }
692                                  }                                  }
693                          }                          }
694                            $i++;
695                  }                  }
                 $i++;  
         }  
696    
697          warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2);                  warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2);
698    
699                    $marc_record->[ $marc_record_offset ] = $marc;
700            }
701    
         $marc_record->[ $marc_record_offset ] = $marc;  
702    
703          warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1);          warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1);
704  }  }
# Line 880  syntaxtic sugar for Line 894  syntaxtic sugar for
894    @v = rec('200')    @v = rec('200')
895    @v = rec('200','a')    @v = rec('200','a')
896    
897    If rec() returns just single value, it will
898    return scalar, not array.
899    
900  =cut  =cut
901    
902  sub rec {  sub rec {
# Line 889  sub rec { Line 906  sub rec {
906          } elsif ($#_ == 1) {          } elsif ($#_ == 1) {
907                  @out = rec2(@_);                  @out = rec2(@_);
908          }          }
909          if (@out) {          if ($#out == 0 && ! wantarray) {
910                    return $out[0];
911            } elsif (@out) {
912                  return @out;                  return @out;
913          } else {          } else {
914                  return '';                  return '';
# Line 1017  Easy as pie, right? Line 1036  Easy as pie, right?
1036  sub lookup {  sub lookup {
1037          my ($what, $database, $input, $key, $having) = @_;          my ($what, $database, $input, $key, $having) = @_;
1038    
1039          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);
1040    
1041          warn "## lookup ($database, $input, $key)", $/ if ($debug > 1);          warn "## lookup ($database, $input, $key)", $/ if ($debug > 1);
1042          return unless (defined($lookup->{$database}->{$input}->{$key}));          return unless (defined($lookup->{$database}->{$input}->{$key}));
# Line 1031  sub lookup { Line 1050  sub lookup {
1050    
1051          foreach my $h ( @having ) {          foreach my $h ( @having ) {
1052                  if (defined($lookup->{$database}->{$input}->{$key}->{$h})) {                  if (defined($lookup->{$database}->{$input}->{$key}->{$h})) {
1053                          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);
1054                          $mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} };                          $mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} };
1055                  }                  }
1056          }          }
# Line 1040  sub lookup { Line 1059  sub lookup {
1059    
1060          my @mfns = sort keys %$mfns;          my @mfns = sort keys %$mfns;
1061    
1062          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);
1063    
1064          my $old_rec = $rec;          my $old_rec = $rec;
1065          my @out;          my @out;
# Line 1048  sub lookup { Line 1067  sub lookup {
1067          foreach my $mfn (@mfns) {          foreach my $mfn (@mfns) {
1068                  $rec = $load_row_coderef->( $database, $input, $mfn );                  $rec = $load_row_coderef->( $database, $input, $mfn );
1069    
1070                  warn "got $database/$input/$mfn = ", dump($rec), $/;                  warn "got $database/$input/$mfn = ", dump($rec), $/ if ($debug);
1071    
1072                  my @vals = $what->();                  my @vals = $what->();
1073    
1074                  push @out, ( @vals );                  push @out, ( @vals );
1075    
1076                  warn "lookup for mfn $mfn returned ", dump(@vals), $/;                  warn "lookup for mfn $mfn returned ", dump(@vals), $/ if ($debug);
1077          }          }
1078    
1079  #       if (ref($lookup->{$k}) eq 'ARRAY') {  #       if (ref($lookup->{$k}) eq 'ARRAY') {
# Line 1065  sub lookup { Line 1084  sub lookup {
1084    
1085          $rec = $old_rec;          $rec = $old_rec;
1086    
1087          warn "## lookup returns = ", dump(@out), $/;          warn "## lookup returns = ", dump(@out), $/ if ($debug);
1088    
1089          if ($#out == 0) {          if ($#out == 0) {
1090                  return $out[0];                  return $out[0];
# Line 1243  sub split_rec_on { Line 1262  sub split_rec_on {
1262          }          }
1263  }  }
1264    
1265    my $hash;
1266    
1267    =head2 set
1268    
1269      set( key => 'value' );
1270    
1271    =cut
1272    
1273    sub set {
1274            my ($k,$v) = @_;
1275            warn "## set ( $k => ", dump($v), " )", $/;
1276            $hash->{$k} = $v;
1277    };
1278    
1279    =head2 get
1280    
1281      get( 'key' );
1282    
1283    =cut
1284    
1285    sub get {
1286            my $k = shift || return;
1287            my $v = $hash->{$k};
1288            warn "## get $k = ", dump( $v ), $/;
1289            return $v;
1290    }
1291    
1292    
1293  # END  # END
1294  1;  1;

Legend:
Removed from v.740  
changed lines
  Added in v.786

  ViewVC Help
Powered by ViewVC 1.1.26