--- trunk/Fast.pm 2005/12/18 23:12:26 6 +++ trunk/Fast.pm 2007/11/04 22:44:42 23 @@ -1,5 +1,5 @@ - package MARC::Fast; + use strict; use Carp; use Data::Dumper; @@ -7,7 +7,7 @@ BEGIN { use Exporter (); use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); - $VERSION = 0.02; + $VERSION = 0.09; @ISA = qw (Exporter); #Give a hoot don't pollute, do not export more than needed by default @EXPORT = qw (); @@ -23,12 +23,21 @@ 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 =head1 DESCRIPTION This is very fast alternative to C and C 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 @@ -42,6 +51,11 @@ quiet => 0, debug => 0, assert => 0, + hash_filter => sub { + my ($t, $record_number) = @_; + $t =~ s/foo/bar/; + return $t; + }, ); =cut @@ -106,7 +120,7 @@ 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); @@ -140,14 +154,22 @@ my $hash = $marc->fetch(42); +First record number is C<1> + =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; @@ -229,11 +251,31 @@ } +=head2 last_leader + +Returns leader of last record Led + + 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 - my $hash = $marc->to_hash($mfn); + my $hash = $marc->to_hash( $mfn, include_subfields => 1, ); It has ability to convert characters (using C) from MARC database before creating structures enabling character re-mapping or quick @@ -260,53 +302,86 @@ my $mfn = shift || confess "need mfn!"; + my $args = {@_}; + # init record to include MFN as field 000 my $rec = { '000' => [ $mfn ] }; my $row = $self->fetch($mfn) || return; - foreach my $k (keys %{$row}) { - foreach my $l (@{$row->{$k}}) { + foreach my $rec_nr (keys %{$row}) { + foreach my $l (@{$row->{$rec_nr}}) { # remove end marker $l =~ s/\x1E$//; # filter output - $l = $self->{'hash_filter'}->($l) if ($self->{'hash_filter'}); + $l = $self->{'hash_filter'}->($l, $rec_nr) if ($self->{'hash_filter'}); my $val; # 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); + + push @subfields, ( $f, $sf_usage->{$f}++ || 0 ); + + # repeatable subfiled -- convert it to array + if ($val->{$f}) { + if ( $sf_usage->{$f} == 2 ) { + $val->{$f} = [ $val->{$f}, $val ]; + } else { + push @{$val->{$f}}, $val; + } + } $val->{substr($t,0,1)} = substr($t,1); } + $val->{subfields} = [ @subfields ] if $args->{include_subfields}; } else { $val = $l; } - push @{$rec->{$k}}, $val; + push @{$rec->{$rec_nr}}, $val; } } return $rec; } +=head2 to_ascii -1; -__END__ + print $marc->to_ascii( 42 ); -=head1 BUGS +=cut +sub to_ascii { + my $self = shift; + my $mfn = shift || confess "need mfn"; + my $row = $self->fetch($mfn) || return; -=head1 SUPPORT + my $out; + foreach my $f (sort keys %{$row}) { + my $dump = join('', @{ $row->{$f} }); + $dump =~ s/\x1e$//; + $dump =~ s/\x1f/\$/g; + $out .= "$f\t$dump\n"; + } + return $out; +} + +1; +__END__ =head1 AUTHOR @@ -326,6 +401,6 @@ =head1 SEE ALSO -perl(1). +L, perl(1). =cut