added to_hash method and hash_filter coderef to new constructor to filter
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 29 Dec 2004 20:10:11 +0000 (20:10 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 29 Dec 2004 20:10:11 +0000 (20:10 +0000)
data prior to unpacking ISIS data into hash.

git-svn-id: file:///home/dpavlin/svn/Biblio-Isis/trunk@12 4670fa4d-42ec-0310-ab5b-a66af6943492

IsisDB.pm

index b7b4ba1..c5c6220 100644 (file)
--- a/IsisDB.pm
+++ b/IsisDB.pm
@@ -7,7 +7,7 @@ use Data::Dumper;
 BEGIN {
        use Exporter ();
        use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
-       $VERSION     = 0.03;
+       $VERSION     = 0.04;
        @ISA         = qw (Exporter);
        #Give a hoot don't pollute, do not export more than needed by default
        @EXPORT      = qw ();
@@ -70,8 +70,12 @@ Open CDS/ISIS database
  my $isis = new IsisDB(
        isisdb => './cds/cds',
        read_fdt => 1,
-       debug => 1,
        include_deleted => 1,
+       hash_filter => sub {
+               my $v = shift;
+               $v =~ s#foo#bar#g;
+       },
+       debug => 1,
  );
 
 Options are described below:
@@ -88,14 +92,18 @@ and common prefix of C<.FDT>, C<.MST>, C<.CNT>, C<.XRF> and C<.MST> files.
 Boolean flag to specify if field definition table should be read. It's off
 by default.
 
-=item debug
-
-Dump a C<lot> of debugging output.
-
 =item include_deleted
 
 Don't skip logically deleted records in ISIS.
 
+=item hash_filter
+
+Filter code ref which will be used before data is converted to hash.
+
+=item debug
+
+Dump a B<lot> of debugging output.
+
 =back
 
 It will also set C<$isis-E<gt>{'maxmfn'}> which is maximum MFN stored in database.
@@ -109,7 +117,7 @@ sub new {
 
        croak "new needs database name (isisdb) as argument!" unless ({@_}->{isisdb});
 
-       foreach my $v (qw{isisdb debug include_deleted}) {
+       foreach my $v (qw{isisdb debug include_deleted hash_filter}) {
                $self->{$v} = {@_}->{$v};
        }
 
@@ -223,7 +231,7 @@ Read record with selected MFN
   my $rec = $isis->fetch(55);
 
 Returns hash with keys which are field names and values are unpacked values
-for that field.
+for that field (like C<^asometing^bsomething else>)
 
 =cut
 
@@ -378,6 +386,69 @@ sub to_ascii {
        return $out;
 }
 
+=head2 to_hash
+
+Read mfn and convert it to hash
+
+  my $hash = $isis->to_hash($mfn);
+
+It has ability to convert characters (using C<hash_filter> from ISIS
+database before creating structures enabling character remapping or quick
+fixup of data.
+
+This function returns hash which is like this:
+
+  $hash = {
+    '210' => [
+               {
+                 'c' => 'New York University press',
+                 'a' => 'New York',
+                 'd' => 'cop. 1988'
+               }
+             ],
+    '990' => [
+               '2140',
+               '88',
+               'HAY'
+             ],
+  };
+
+You can later use that has to produce any output from ISIS data.
+
+=cut
+
+sub to_hash {
+       my $self = shift;
+
+       my $mfn = shift || confess "need mfn!";
+
+       my $rec;
+       my $row = $self->fetch($mfn);
+
+       foreach my $k (keys %{$row}) {
+               foreach my $l (@{$row->{$k}}) {
+
+                       # filter output
+                       $l = $self->{'hash_filter'}->($l) if ($self->{'hash_filter'});
+
+                       # has subfields?
+                       my $val;
+                       if ($l =~ m/\^/) {
+                               foreach my $t (split(/\^/,$l)) {
+                                       next if (! $t);
+                                       $val->{substr($t,0,1)} = substr($t,1);
+                               }
+                       } else {
+                               $val = $l;
+                       }
+
+                       push @{$rec->{$k}}, $val;
+               }
+       }
+
+       return $rec;
+}
+
 #
 # XXX porting from php left-over:
 #