much more sane implementation of to_hash which now include
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 4 Nov 2007 22:44:42 +0000 (22:44 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 4 Nov 2007 22:44:42 +0000 (22:44 +0000)
option include_subfields just like Biblio::Isis does. [0.09]

git-svn-id: svn+ssh://llin/home/dpavlin/private/svn/MARC-Fast/trunk@23 49f9634a-d7ec-0310-8e6b-ec35c6cc8804

Fast.pm
t/001_marc.t

diff --git a/Fast.pm b/Fast.pm
index b587be3..aa510ba 100644 (file)
--- a/Fast.pm
+++ b/Fast.pm
@@ -7,7 +7,7 @@ use Data::Dumper;
 BEGIN {
        use Exporter ();
        use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 BEGIN {
        use Exporter ();
        use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
-       $VERSION     = 0.08;
+       $VERSION     = 0.09;
        @ISA         = qw (Exporter);
        #Give a hoot don't pollute, do not export more than needed by default
        @EXPORT      = qw ();
        @ISA         = qw (Exporter);
        #Give a hoot don't pollute, do not export more than needed by default
        @EXPORT      = qw ();
@@ -275,7 +275,7 @@ sub last_leader {
 
 Read record with specified MFN and convert it to hash
 
 
 Read record with specified MFN and convert it to hash
 
-  my $hash = $marc->to_hash($mfn);
+  my $hash = $marc->to_hash( $mfn, include_subfields => 1, );
 
 It has ability to convert characters (using C<hash_filter>) from MARC
 database before creating structures enabling character re-mapping or quick
 
 It has ability to convert characters (using C<hash_filter>) from MARC
 database before creating structures enabling character re-mapping or quick
@@ -302,6 +302,8 @@ sub to_hash {
 
        my $mfn = shift || confess "need mfn!";
 
 
        my $mfn = shift || confess "need mfn!";
 
+       my $args = {@_};
+
        # init record to include MFN as field 000
        my $rec = { '000' => [ $mfn ] };
 
        # init record to include MFN as field 000
        my $rec = { '000' => [ $mfn ] };
 
@@ -321,24 +323,28 @@ sub to_hash {
                        # has identifiers?
                        ($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\x1F/\x1F/);
 
                        # has identifiers?
                        ($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\x1F/\x1F/);
 
+                       my $sf_usage;
+                       my @subfields;
+
                        # has subfields?
                        if ($l =~ m/\x1F/) {
                                foreach my $t (split(/\x1F/,$l)) {
                                        next if (! $t);
                                        my $f = substr($t,0,1);
                        # has subfields?
                        if ($l =~ m/\x1F/) {
                                foreach my $t (split(/\x1F/,$l)) {
                                        next if (! $t);
                                        my $f = substr($t,0,1);
-                                       # repeatable subfileds. When we hit first one,
-                                       # store CURRENT (up to that) in first repetition
-                                       # of this record. Then, new record with same
-                                       # identifiers will be created.
+
+                                       push @subfields, ( $f, $sf_usage->{$f}++ || 0 );
+
+                                       # repeatable subfiled -- convert it to array
                                        if ($val->{$f}) {
                                        if ($val->{$f}) {
-                                               push @{$rec->{$rec_nr}}, $val;
-                                               $val = {
-                                                       i1 => $val->{i1},
-                                                       i2 => $val->{i2},
-                                               };
+                                               if ( $sf_usage->{$f} == 2 ) {
+                                                       $val->{$f} = [ $val->{$f}, $val ];
+                                               } else {
+                                                       push @{$val->{$f}}, $val;
+                                               }
                                        }
                                        $val->{substr($t,0,1)} = substr($t,1);
                                }
                                        }
                                        $val->{substr($t,0,1)} = substr($t,1);
                                }
+                               $val->{subfields} = [ @subfields ] if $args->{include_subfields};
                        } else {
                                $val = $l;
                        }
                        } else {
                                $val = $l;
                        }
index 5090234..c05fcd6 100755 (executable)
@@ -3,7 +3,7 @@
 use strict;
 use blib;
 
 use strict;
 use blib;
 
-use Test::More tests => 53;
+use Test::More tests => 63;
 use Test::Exception;
 
 BEGIN {
 use Test::Exception;
 
 BEGIN {
@@ -50,16 +50,20 @@ SKIP: {
        ok(! $marc->fetch($marc->count + 1), "fetch max+1:".($marc->count+1));
 
        foreach (1 .. 10) {
        ok(! $marc->fetch($marc->count + 1), "fetch max+1:".($marc->count+1));
 
        foreach (1 .. 10) {
-               ok($marc->fetch($_), "fetch $_");
+               ok($marc->fetch($_), "fetch($_)");
 
                ok($marc->last_leader, "last_leader $_");
 
 
                ok($marc->last_leader, "last_leader $_");
 
-               ok(my $hash = $marc->to_hash($_), "to_hash $_");
+               ok(my $hash = $marc->to_hash($_), "to_hash($_)");
                diag "to_hash($_) = ",Data::Dump::dump($hash) if ($debug);
                diag "to_hash($_) = ",Data::Dump::dump($hash) if ($debug);
-               ok(my $ascii = $marc->to_ascii($_), "to_ascii $_");
+
+               ok(my $hash_sf = $marc->to_hash($_, include_subfields => 1), "to_hash($_,include_subfields)");
+               diag "to_hash($_, include_subfields => 1) = ",Data::Dump::dump($hash_sf) if ($debug);
+
+               ok(my $ascii = $marc->to_ascii($_), "to_ascii($_)");
                diag "to_ascii($_) ::\n$ascii" if ($debug);
        }
 
                diag "to_ascii($_) ::\n$ascii" if ($debug);
        }
 
-       ok(! $marc->fetch(0), "fetch 0 again");
+       ok(! $marc->fetch(0), "fetch(0) again");
        ok(! $marc->last_leader, "no last_leader");
 }
        ok(! $marc->last_leader, "no last_leader");
 }