/[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 67 by dpavlin, Fri Aug 25 16:35:47 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.23;
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    =item hash_filter
552    
553    You can override C<hash_filter> defined in L</new> using this option.
554    
555    =back
556    
557  =cut  =cut
558    
559  sub to_hash {  sub to_hash {
560          my $self = shift;          my $self = shift;
561    
562    
563          my $mfn = shift || confess "need mfn!";          my $mfn = shift || confess "need mfn!";
564            my $arg;
565    
566            my $hash_filter = $self->{hash_filter};
567    
568            if (ref($mfn) eq 'HASH') {
569                    $arg = $mfn;
570                    $mfn = $arg->{mfn} || confess "need mfn in arguments";
571                    $hash_filter = $arg->{hash_filter} if ($arg->{hash_filter});
572            }
573    
574          # init record to include MFN as field 000          # init record to include MFN as field 000
575          my $rec = { '000' => [ $mfn ] };          my $rec = { '000' => [ $mfn ] };
576    
577          my $row = $self->fetch($mfn) || return;          my $row = $self->fetch($mfn) || return;
578    
579          foreach my $k (keys %{$row}) {          my $j_rs = $arg->{join_subfields_with};
580                  foreach my $l (@{$row->{$k}}) {          $j_rs = $self->{join_subfields_with} unless(defined($j_rs));
581            my $i_sf = $arg->{include_subfields};
582    
583            foreach my $f_nr (keys %{$row}) {
584                    foreach my $l (@{$row->{$f_nr}}) {
585    
586                          # filter output                          # filter output
587                          if ($self->{'hash_filter'}) {                          $l = $hash_filter->($l, $f_nr) if ($hash_filter);
588                                  $l = $self->{'hash_filter'}->($l);                          next unless defined($l);
                                 next unless defined($l);  
                         }  
589    
590                          my $val;                          my $val;
591                            my $r_sf;       # repeatable subfields in this record
592    
593                          # has identifiers?                          # has identifiers?
594                          ($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 598  sub to_hash {
598                                  foreach my $t (split(/\^/,$l)) {                                  foreach my $t (split(/\^/,$l)) {
599                                          next if (! $t);                                          next if (! $t);
600                                          my ($sf,$v) = (substr($t,0,1), substr($t,1));                                          my ($sf,$v) = (substr($t,0,1), substr($t,1));
601                                          # FIXME make this option !                                          # XXX this might be option, but why?
602                                          next unless ($v);                                          next unless (defined($v) && $v ne '');
603  #                                       warn "### $k^$sf:$v",$/ if ($self->{debug} > 1);  #                                       warn "### $f_nr^$sf:$v",$/ if ($self->{debug} > 1);
604    
                                         # FIXME array return optional, by default unroll to ' ; '  
605                                          if (ref( $val->{$sf} ) eq 'ARRAY') {                                          if (ref( $val->{$sf} ) eq 'ARRAY') {
606    
607                                                  push @{ $val->{$sf} }, $v;                                                  push @{ $val->{$sf} }, $v;
608    
609                                                    # record repeatable subfield it it's offset
610                                                    push @{ $val->{subfields} }, ( $sf, $#{ $val->{$sf} } ) if (! $j_rs && $i_sf);
611                                                    $r_sf->{$sf}++;
612    
613                                          } elsif (defined( $val->{$sf} )) {                                          } elsif (defined( $val->{$sf} )) {
614    
615                                                  # convert scalar field to array                                                  # convert scalar field to array
616                                                  $val->{$sf} = [ $val->{$sf}, $v ];                                                  $val->{$sf} = [ $val->{$sf}, $v ];
617    
618                                                    push @{ $val->{subfields} }, ( $sf, 1 ) if (! $j_rs && $i_sf);
619                                                    $r_sf->{$sf}++;
620    
621                                          } else {                                          } else {
622                                                  $val->{$sf} = $v;                                                  $val->{$sf} = $v;
623                                                    push @{ $val->{subfields} }, ( $sf, 0 ) if ($i_sf);
624                                          }                                          }
625                                  }                                  }
626                          } else {                          } else {
627                                  $val = $l;                                  $val = $l;
628                          }                          }
629    
630                          push @{$rec->{$k}}, $val;                          if ($j_rs) {
631                                    map {
632                                            $val->{$_} = join($j_rs, @{ $val->{$_} });
633                                    } keys %$r_sf
634                            }
635    
636                            push @{$rec->{$f_nr}}, $val;
637                  }                  }
638          }          }
639    
# Line 656  know any details about it's version. Line 748  know any details about it's version.
748    
749  =head1 VERSIONS  =head1 VERSIONS
750    
751  You can find version dependencies documented here  As this is young module, new features are added in subsequent version. It's
752    a good idea to specify version when using this module like this:
753    
754      use Biblio::Isis 0.23
755    
756    Below is list of changes in specific version of module (so you can target
757    older versions if you really have to):
758    
759  =over 8  =over 8
760    
761    =item 0.23
762    
763    Added C<hash_filter> to L</to_hash>
764    
765    =item 0.22
766    
767    Added field number when calling C<hash_filter>
768    
769    =item 0.21
770    
771    Added C<join_subfields_with> to L</new> and L</to_hash>.
772    
773    Added C<include_subfields> to L</to_hash>.
774    
775  =item 0.20  =item 0.20
776    
777  Added C<< $isis->mfn >> and support for repeatable subfields  Added C<< $isis->mfn >>, support for repeatable subfields and
778    C<< $isis->to_hash({ mfn => 42, ... }) >> calling convention
779    
780  =back  =back
781    
# Line 687  LICENSE file included with this module. Line 800  LICENSE file included with this module.
800    
801  =head1 SEE ALSO  =head1 SEE ALSO
802    
803    L<Biblio::Isis::Manual> for CDS/ISIS manual appendix F, G and H which describe file format
804    
805  OpenIsis web site L<http://www.openisis.org>  OpenIsis web site L<http://www.openisis.org>
806    
807  perl4lib site L<http://perl4lib.perl.org>  perl4lib site L<http://perl4lib.perl.org>

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

  ViewVC Help
Powered by ViewVC 1.1.26