/[Biblio-Isis]/trunk/lib/Biblio/Isis.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/Biblio/Isis.pm

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

revision 54 by dpavlin, Fri Jul 7 23:45:12 2006 UTC revision 65 by dpavlin, Thu Jul 13 13:34:30 2006 UTC
# Line 7  use File::Glob qw(:globally :nocase); Line 7  use File::Glob qw(:globally :nocase);
7  BEGIN {  BEGIN {
8          use Exporter ();          use Exporter ();
9          use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);          use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
10          $VERSION     = 0.20;          $VERSION     = 0.22;
11          @ISA         = qw (Exporter);          @ISA         = qw (Exporter);
12          #Give a hoot don't pollute, do not export more than needed by default          #Give a hoot don't pollute, do not export more than needed by default
13          @EXPORT      = qw ();          @EXPORT      = qw ();
# Line 84  Open ISIS database Line 84  Open ISIS database
84          read_fdt => 1,          read_fdt => 1,
85          include_deleted => 1,          include_deleted => 1,
86          hash_filter => sub {          hash_filter => sub {
87                  my $v = shift;                  my ($v,$field_number) = @_;
88                  $v =~ s#foo#bar#g;                  $v =~ s#foo#bar#g;
89          },          },
90          debug => 1,          debug => 1,
91            join_subfields_with => ' ; ',
92   );   );
93    
94  Options are described below:  Options are described below:
# Line 113  Don't skip logically deleted records in Line 114  Don't skip logically deleted records in
114    
115  =item hash_filter  =item hash_filter
116    
117  Filter code ref which will be used before data is converted to hash.  Filter code ref which will be used before data is converted to hash. It will
118    receive two arguments, whole line from current field (in C<< $_[0] >>) and
119    field number (in C<< $_[1] >>).
120    
121  =item debug  =item debug
122    
123  Dump a B<lot> of debugging output even at level 1. For even more increase level.  Dump a B<lot> of debugging output even at level 1. For even more increase level.
124    
125    =item join_subfields_with
126    
127    Define delimiter which will be used to join repeatable subfields. This
128    option is included to support lagacy application written against version
129    older than 0.21 of this module. By default, it disabled. See L</to_hash>.
130    
131  =back  =back
132    
133  =cut  =cut
# Line 489  following structure: Line 498  following structure:
498          'a' => [ 'foo', 'bar', 'baz' ],          'a' => [ 'foo', 'bar', 'baz' ],
499    }]    }]
500    
501    Or in more complex example of
502    
503      902   ^aa1^aa2^aa3^bb1^aa4^bb2^cc1^aa5
504    
505    it will create
506    
507      902   => [
508            { a => ["a1", "a2", "a3", "a4", "a5"], b => ["b1", "b2"], c => "c1" },
509      ],
510    
511    This behaviour can be changed using C<join_subfields_with> option to L</new>,
512    in which case C<to_hash> will always create single value for each subfield.
513    This will change result to:
514    
515    
516    
517  This method will also create additional field C<000> with MFN.  This method will also create additional field C<000> with MFN.
518    
519    There is also more elaborative way to call C<to_hash> like this:
520    
521      my $hash = $isis->to_hash({
522            mfn => 42,
523            include_subfields => 1,
524      });
525    
526    Each option controll creation of hash:
527    
528    =over 4
529    
530    =item mfn
531    
532    Specify MFN number of record
533    
534    =item include_subfields
535    
536    This option will create additional key in hash called C<subfields> which will
537    have original record subfield order and index to that subfield like this:
538    
539      902   => [ {
540            a => ["a1", "a2", "a3", "a4", "a5"],
541            b => ["b1", "b2"],
542            c => "c1",
543            subfields => ["a", 0, "a", 1, "a", 2, "b", 0, "a", 3, "b", 1, "c", 0, "a", 4],
544      } ],
545    
546    =item join_subfields_with
547    
548    Define delimiter which will be used to join repeatable subfields. You can
549    specify option here instead in L</new> if you want to have per-record control.
550    
551    =back
552    
553  =cut  =cut
554    
555  sub to_hash {  sub to_hash {
556          my $self = shift;          my $self = shift;
557    
558    
559          my $mfn = shift || confess "need mfn!";          my $mfn = shift || confess "need mfn!";
560            my $arg;
561    
562            if (ref($mfn) eq 'HASH') {
563                    $arg = $mfn;
564                    $mfn = $arg->{mfn} || confess "need mfn in arguments";
565            }
566    
567          # init record to include MFN as field 000          # init record to include MFN as field 000
568          my $rec = { '000' => [ $mfn ] };          my $rec = { '000' => [ $mfn ] };
569    
570          my $row = $self->fetch($mfn) || return;          my $row = $self->fetch($mfn) || return;
571    
572          foreach my $k (keys %{$row}) {          my $j_rs = $arg->{join_subfields_with};
573                  foreach my $l (@{$row->{$k}}) {          $j_rs = $self->{join_subfields_with} unless(defined($j_rs));
574            my $i_sf = $arg->{include_subfields};
575    
576            foreach my $f_nr (keys %{$row}) {
577                    foreach my $l (@{$row->{$f_nr}}) {
578    
579                          # filter output                          # filter output
580                          if ($self->{'hash_filter'}) {                          if ($self->{'hash_filter'}) {
581                                  $l = $self->{'hash_filter'}->($l);                                  $l = $self->{'hash_filter'}->($l, $f_nr);
582                                  next unless defined($l);                                  next unless defined($l);
583                          }                          }
584    
585                          my $val;                          my $val;
586                            my $r_sf;       # repeatable subfields in this record
587    
588                          # has identifiers?                          # has identifiers?
589                          ($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\^/\^/);                          ($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\^/\^/);
# Line 522  sub to_hash { Line 593  sub to_hash {
593                                  foreach my $t (split(/\^/,$l)) {                                  foreach my $t (split(/\^/,$l)) {
594                                          next if (! $t);                                          next if (! $t);
595                                          my ($sf,$v) = (substr($t,0,1), substr($t,1));                                          my ($sf,$v) = (substr($t,0,1), substr($t,1));
596                                          # FIXME make this option !                                          # XXX this might be option, but why?
597                                          next unless ($v);                                          next unless ($v);
598  #                                       warn "### $k^$sf:$v",$/ if ($self->{debug} > 1);  #                                       warn "### $f_nr^$sf:$v",$/ if ($self->{debug} > 1);
599    
                                         # FIXME array return optional, by default unroll to ' ; '  
600                                          if (ref( $val->{$sf} ) eq 'ARRAY') {                                          if (ref( $val->{$sf} ) eq 'ARRAY') {
601    
602                                                  push @{ $val->{$sf} }, $v;                                                  push @{ $val->{$sf} }, $v;
603    
604                                                    # record repeatable subfield it it's offset
605                                                    push @{ $val->{subfields} }, ( $sf, $#{ $val->{$sf} } ) if (! $j_rs && $i_sf);
606                                                    $r_sf->{$sf}++;
607    
608                                          } elsif (defined( $val->{$sf} )) {                                          } elsif (defined( $val->{$sf} )) {
609    
610                                                  # convert scalar field to array                                                  # convert scalar field to array
611                                                  $val->{$sf} = [ $val->{$sf}, $v ];                                                  $val->{$sf} = [ $val->{$sf}, $v ];
612    
613                                                    push @{ $val->{subfields} }, ( $sf, 1 ) if (! $j_rs && $i_sf);
614                                                    $r_sf->{$sf}++;
615    
616                                          } else {                                          } else {
617                                                  $val->{$sf} = $v;                                                  $val->{$sf} = $v;
618                                                    push @{ $val->{subfields} }, ( $sf, 0 ) if ($i_sf);
619                                          }                                          }
620                                  }                                  }
621                          } else {                          } else {
622                                  $val = $l;                                  $val = $l;
623                          }                          }
624    
625                          push @{$rec->{$k}}, $val;                          if ($j_rs) {
626                                    map {
627                                            $val->{$_} = join($j_rs, @{ $val->{$_} });
628                                    } keys %$r_sf
629                            }
630    
631                            push @{$rec->{$f_nr}}, $val;
632                  }                  }
633          }          }
634    
# Line 656  know any details about it's version. Line 743  know any details about it's version.
743    
744  =head1 VERSIONS  =head1 VERSIONS
745    
746  You can find version dependencies documented here  As this is young module, new features are added in subsequent version. It's
747    a good idea to specify version when using this module like this:
748    
749      use Biblio::Isis 0.21
750    
751    Below is list of changes in specific version of module (so you can target
752    older versions if you really have to):
753    
754  =over 8  =over 8
755    
756    =item 0.22
757    
758    Added field number when calling C<hash_filter>
759    
760    =item 0.21
761    
762    Added C<join_subfields_with> to L</new> and L</to_hash>.
763    
764    Added C<include_subfields> to L</to_hash>.
765    
766  =item 0.20  =item 0.20
767    
768  Added C<< $isis->mfn >> and support for repeatable subfields  Added C<< $isis->mfn >>, support for repeatable subfields and
769    C<< $isis->to_hash({ mfn => 42, ... }) >> calling convention
770    
771  =back  =back
772    
# Line 687  LICENSE file included with this module. Line 791  LICENSE file included with this module.
791    
792  =head1 SEE ALSO  =head1 SEE ALSO
793    
794    L<Biblio::Isis::Manual> for CDS/ISIS manual appendix F, G and H which describe file format
795    
796  OpenIsis web site L<http://www.openisis.org>  OpenIsis web site L<http://www.openisis.org>
797    
798  perl4lib site L<http://perl4lib.perl.org>  perl4lib site L<http://perl4lib.perl.org>

Legend:
Removed from v.54  
changed lines
  Added in v.65

  ViewVC Help
Powered by ViewVC 1.1.26