--- trunk/Fast.pm 2005/10/08 16:33:09 5 +++ trunk/Fast.pm 2007/06/21 10:24:12 17 @@ -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.07; @ISA = qw (Exporter); #Give a hoot don't pollute, do not export more than needed by default @EXPORT = qw (); @@ -42,6 +42,11 @@ quiet => 0, debug => 0, assert => 0, + hash_filter => sub { + my ($t, $record_number) = @_; + $t =~ s/foo/bar/; + return $t; + }, ); =cut @@ -59,6 +64,7 @@ print STDERR "# opening ",$self->{marcdb},"\n" if ($self->{debug}); open($self->{fh}, $self->{marcdb}) || croak "can't open ",$self->{marcdb},": $!"; + binmode($self->{fh}); $self->{count} = 0; @@ -69,7 +75,12 @@ push @{$self->{fh_offset}}, tell($self->{fh}); my $leader; - read($self->{fh}, $leader, 24); + my $len = read($self->{fh}, $leader, 24); + + if ($len < 24) { + carp "short read of leader, aborting\n"; + last; + } # Byte Name # ---- ---- @@ -103,7 +114,12 @@ push @{$self->{leaders}}, $leader; # skip to next record - seek($self->{fh},substr($leader,0,5)-24,1); + my $o = substr($leader,0,5); + if ($o > 24) { + seek($self->{fh},$o-24,1) if ($o); + } else { + last; + } } @@ -196,11 +212,7 @@ my $f = substr($fields,$addr,$len); print STDERR "tag/len/addr $tag [$len] $addr: '$f'\n" if ($self->{debug}); - if ($row->{$tag}) { - $row->{$tag} .= $f; - } else { - $row->{$tag} = $f; - } + push @{ $row->{$tag} }, $f; my $del = substr($fields,$addr+$len-1,1); @@ -221,16 +233,112 @@ return $row; } -1; -__END__ -=head1 BUGS +=head2 to_hash + +Read record with specified MFN and convert it to hash + + my $hash = $marc->to_hash($mfn); + +It has ability to convert characters (using C) from MARC +database before creating structures enabling character re-mapping or quick +fix-up of data. + +This function returns hash which is like this: + + '200' => [ + { + 'i1' => '1', + 'i2' => ' ' + 'a' => 'Goa', + 'f' => 'Valdo D\'Arienzo', + 'e' => 'tipografie e tipografi nel XVI secolo', + } + ], + +This method will also create additional field C<000> with MFN. + +=cut + +sub to_hash { + my $self = shift; + my $mfn = shift || confess "need mfn!"; + # init record to include MFN as field 000 + my $rec = { '000' => [ $mfn ] }; -=head1 SUPPORT + my $row = $self->fetch($mfn) || return; + 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, $rec_nr) if ($self->{'hash_filter'}); + + my $val; + + # has identifiers? + ($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\x1F/\x1F/); + + # 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. + if ($val->{$f}) { + push @{$rec->{$rec_nr}}, $val; + $val = { + i1 => $val->{i1}, + i2 => $val->{i2}, + }; + } + $val->{substr($t,0,1)} = substr($t,1); + } + } else { + $val = $l; + } + push @{$rec->{$rec_nr}}, $val; + } + } + + return $rec; +} + +=head2 to_ascii + + print $marc->to_ascii( 42 ); + +=cut + +sub to_ascii { + my $self = shift; + + my $mfn = shift || confess "need mfn"; + my $row = $self->fetch($mfn) || return; + + 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 @@ -250,6 +358,6 @@ =head1 SEE ALSO -perl(1). +L, perl(1). =cut