added $marc->last_leader which returns leader of
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 29 Oct 2007 22:33:35 +0000 (22:33 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 29 Oct 2007 22:33:35 +0000 (22:33 +0000)
last record accessed with $marc->fetch
bump version to [0.08]

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

Fast.pm
t/001_marc.t

diff --git a/Fast.pm b/Fast.pm
index 982c366..b587be3 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.07;
+       $VERSION     = 0.08;
        @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 ();
@@ -23,12 +23,21 @@ MARC::Fast - Very fast implementation of MARC database reader
 
   use MARC::Fast;
 
 
   use MARC::Fast;
 
+  my $marc = new MARC::Fast(
+       marcdb => 'unimarc.iso',
+  );
+
+  foreach my $mfn ( 1 .. $marc->count ) {
+       print $marc->to_ascii( $mfn );
+  }
+
+For longer example with command line options look at L<scripts/dump_fastmarc.pl>
 
 =head1 DESCRIPTION
 
 This is very fast alternative to C<MARC> and C<MARC::Record> modules.
 
 
 =head1 DESCRIPTION
 
 This is very fast alternative to C<MARC> and C<MARC::Record> modules.
 
-It's is also very sutable for random access to MARC records (as opposed to
+It's is also very subtable for random access to MARC records (as opposed to
 sequential one).
 
 =head1 METHODS
 sequential one).
 
 =head1 METHODS
@@ -111,7 +120,7 @@ sub new {
                print STDERR "REC ",$self->{count},": $leader\n" if ($self->{debug});
 
                # store leader for later
                print STDERR "REC ",$self->{count},": $leader\n" if ($self->{debug});
 
                # store leader for later
-               push @{$self->{leaders}}, $leader;
+               push @{$self->{leader}}, $leader;
 
                # skip to next record
                my $o = substr($leader,0,5);
 
                # skip to next record
                my $o = substr($leader,0,5);
@@ -145,14 +154,22 @@ Fetch record from database
 
   my $hash = $marc->fetch(42);
 
 
   my $hash = $marc->fetch(42);
 
+First record number is C<1>
+
 =cut
 
 sub fetch {
        my $self = shift;
 
 =cut
 
 sub fetch {
        my $self = shift;
 
-       my $rec_nr = shift || return;
+       my $rec_nr = shift;
 
 
-       my $leader = $self->{leaders}->[$rec_nr - 1];
+       if ( ! $rec_nr ) {
+               $self->{last_leader} = undef;
+               return;
+       }
+
+       my $leader = $self->{leader}->[$rec_nr - 1];
+       $self->{last_leader} = $leader;
        unless ($leader) {
                carp "can't find record $rec_nr";
                return;
        unless ($leader) {
                carp "can't find record $rec_nr";
                return;
@@ -234,6 +251,26 @@ sub fetch {
 }
 
 
 }
 
 
+=head2 last_leader
+
+Returns leader of last record L<fetch>ed
+
+  print $marc->last_leader;
+
+Added in version 0.08 of this module, so if you need it use:
+
+  use MARC::Fast 0.08;
+
+to be sure that it's supported.
+
+=cut
+
+sub last_leader {
+       my $self = shift;
+       return $self->{last_leader};
+}
+
+
 =head2 to_hash
 
 Read record with specified MFN and convert it to hash
 =head2 to_hash
 
 Read record with specified MFN and convert it to hash
index 3553dc3..990d078 100755 (executable)
@@ -3,7 +3,7 @@
 use strict;
 use blib;
 
 use strict;
 use blib;
 
-use Test::More tests => 40;
+use Test::More tests => 53;
 use Test::Exception;
 
 BEGIN {
 use Test::Exception;
 
 BEGIN {
@@ -36,19 +36,25 @@ SKIP: {
 
        #diag Dumper($marc);
 
 
        #diag Dumper($marc);
 
-       cmp_ok($marc->count, '==', scalar @{$marc->{leaders}}, "count == leaders");
+       cmp_ok($marc->count, '==', scalar @{$marc->{leader}}, "count == leader");
        cmp_ok($marc->count, '==', scalar @{$marc->{fh_offset}}, "count == fh_offset");
 
        ok(! $marc->fetch(0), "fetch 0");
        cmp_ok($marc->count, '==', scalar @{$marc->{fh_offset}}, "count == fh_offset");
 
        ok(! $marc->fetch(0), "fetch 0");
+       ok(! $marc->last_leader, "no last_leader");
        ok($marc->fetch($marc->count), "fetch max:".$marc->count);
        ok(! $marc->fetch($marc->count + 1), "fetch max+1:".($marc->count+1));
 
        foreach (1 .. 10) {
                ok($marc->fetch($_), "fetch $_");
 
        ok($marc->fetch($marc->count), "fetch max:".$marc->count);
        ok(! $marc->fetch($marc->count + 1), "fetch max+1:".($marc->count+1));
 
        foreach (1 .. 10) {
                ok($marc->fetch($_), "fetch $_");
 
+               ok($marc->last_leader, "last_leader $_");
+
                ok(my $hash = $marc->to_hash($_), "to_hash $_");
                diag "to_hash($_) = ",Data::Dump::dump($hash) if ($debug);
                ok(my $ascii = $marc->to_ascii($_), "to_ascii $_");
                diag "to_ascii($_) ::\n$ascii" if ($debug);
        }
                ok(my $hash = $marc->to_hash($_), "to_hash $_");
                diag "to_hash($_) = ",Data::Dump::dump($hash) if ($debug);
                ok(my $ascii = $marc->to_ascii($_), "to_ascii $_");
                diag "to_ascii($_) ::\n$ascii" if ($debug);
        }
+
+       ok(! $marc->fetch(0), "fetch 0 again");
+       ok(! $marc->last_leader, "no last_leader");
 }
 }