|
|
|
1 |
package MARC::Fast; |
package MARC::Fast; |
2 |
|
|
3 |
use strict; |
use strict; |
4 |
use Carp; |
use Carp; |
5 |
use Data::Dumper; |
use Data::Dump qw/dump/; |
6 |
|
|
7 |
BEGIN { |
BEGIN { |
8 |
use Exporter (); |
use Exporter (); |
9 |
use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
10 |
$VERSION = 0.03; |
$VERSION = 0.09; |
11 |
@ISA = qw (Exporter); |
@ISA = qw (Exporter); |
12 |
#Give a hoot don't pollute, do not export more than needed by default |
#Give a hoot don't pollute, do not export more than needed by default |
13 |
@EXPORT = qw (); |
@EXPORT = qw (); |
23 |
|
|
24 |
use MARC::Fast; |
use MARC::Fast; |
25 |
|
|
26 |
|
my $marc = new MARC::Fast( |
27 |
|
marcdb => 'unimarc.iso', |
28 |
|
); |
29 |
|
|
30 |
|
foreach my $mfn ( 1 .. $marc->count ) { |
31 |
|
print $marc->to_ascii( $mfn ); |
32 |
|
} |
33 |
|
|
34 |
|
For longer example with command line options look at L<scripts/dump_fastmarc.pl> |
35 |
|
|
36 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
37 |
|
|
38 |
This is very fast alternative to C<MARC> and C<MARC::Record> modules. |
This is very fast alternative to C<MARC> and C<MARC::Record> modules. |
39 |
|
|
40 |
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 |
41 |
sequential one). |
sequential one). |
42 |
|
|
43 |
=head1 METHODS |
=head1 METHODS |
52 |
debug => 0, |
debug => 0, |
53 |
assert => 0, |
assert => 0, |
54 |
hash_filter => sub { |
hash_filter => sub { |
55 |
my $t = shift; |
my ($t, $record_number) = @_; |
56 |
$t =~ s/foo/bar/; |
$t =~ s/foo/bar/; |
57 |
return $t; |
return $t; |
58 |
}, |
}, |
120 |
print STDERR "REC ",$self->{count},": $leader\n" if ($self->{debug}); |
print STDERR "REC ",$self->{count},": $leader\n" if ($self->{debug}); |
121 |
|
|
122 |
# store leader for later |
# store leader for later |
123 |
push @{$self->{leaders}}, $leader; |
push @{$self->{leader}}, $leader; |
124 |
|
|
125 |
# skip to next record |
# skip to next record |
126 |
my $o = substr($leader,0,5); |
my $o = substr($leader,0,5); |
127 |
|
warn "# in record ", $self->{count}," record length isn't number but: ",dump($o),"\n" unless $o =~ m/^\d+$/; |
128 |
if ($o > 24) { |
if ($o > 24) { |
129 |
seek($self->{fh},$o-24,1) if ($o); |
seek($self->{fh},$o-24,1) if ($o); |
130 |
} else { |
} else { |
155 |
|
|
156 |
my $hash = $marc->fetch(42); |
my $hash = $marc->fetch(42); |
157 |
|
|
158 |
|
First record number is C<1> |
159 |
|
|
160 |
=cut |
=cut |
161 |
|
|
162 |
sub fetch { |
sub fetch { |
163 |
my $self = shift; |
my $self = shift; |
164 |
|
|
165 |
my $rec_nr = shift || return; |
my $rec_nr = shift; |
166 |
|
|
167 |
|
if ( ! $rec_nr ) { |
168 |
|
$self->{last_leader} = undef; |
169 |
|
return; |
170 |
|
} |
171 |
|
|
172 |
my $leader = $self->{leaders}->[$rec_nr - 1]; |
my $leader = $self->{leader}->[$rec_nr - 1]; |
173 |
|
$self->{last_leader} = $leader; |
174 |
unless ($leader) { |
unless ($leader) { |
175 |
carp "can't find record $rec_nr"; |
carp "can't find record $rec_nr"; |
176 |
return; |
return; |
252 |
} |
} |
253 |
|
|
254 |
|
|
255 |
|
=head2 last_leader |
256 |
|
|
257 |
|
Returns leader of last record L<fetch>ed |
258 |
|
|
259 |
|
print $marc->last_leader; |
260 |
|
|
261 |
|
Added in version 0.08 of this module, so if you need it use: |
262 |
|
|
263 |
|
use MARC::Fast 0.08; |
264 |
|
|
265 |
|
to be sure that it's supported. |
266 |
|
|
267 |
|
=cut |
268 |
|
|
269 |
|
sub last_leader { |
270 |
|
my $self = shift; |
271 |
|
return $self->{last_leader}; |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
=head2 to_hash |
=head2 to_hash |
276 |
|
|
277 |
Read record with specified MFN and convert it to hash |
Read record with specified MFN and convert it to hash |
278 |
|
|
279 |
my $hash = $marc->to_hash($mfn); |
my $hash = $marc->to_hash( $mfn, include_subfields => 1, ); |
280 |
|
|
281 |
It has ability to convert characters (using C<hash_filter>) from MARC |
It has ability to convert characters (using C<hash_filter>) from MARC |
282 |
database before creating structures enabling character re-mapping or quick |
database before creating structures enabling character re-mapping or quick |
303 |
|
|
304 |
my $mfn = shift || confess "need mfn!"; |
my $mfn = shift || confess "need mfn!"; |
305 |
|
|
306 |
|
my $args = {@_}; |
307 |
|
|
308 |
# init record to include MFN as field 000 |
# init record to include MFN as field 000 |
309 |
my $rec = { '000' => [ $mfn ] }; |
my $rec = { '000' => [ $mfn ] }; |
310 |
|
|
311 |
my $row = $self->fetch($mfn) || return; |
my $row = $self->fetch($mfn) || return; |
312 |
|
|
313 |
foreach my $k (keys %{$row}) { |
foreach my $rec_nr (keys %{$row}) { |
314 |
foreach my $l (@{$row->{$k}}) { |
foreach my $l (@{$row->{$rec_nr}}) { |
315 |
|
|
316 |
# remove end marker |
# remove end marker |
317 |
$l =~ s/\x1E$//; |
$l =~ s/\x1E$//; |
318 |
|
|
319 |
# filter output |
# filter output |
320 |
$l = $self->{'hash_filter'}->($l) if ($self->{'hash_filter'}); |
$l = $self->{'hash_filter'}->($l, $rec_nr) if ($self->{'hash_filter'}); |
321 |
|
|
322 |
my $val; |
my $val; |
323 |
|
|
324 |
# has identifiers? |
# has identifiers? |
325 |
($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\x1F/\x1F/); |
($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\x1F/\x1F/); |
326 |
|
|
327 |
|
my $sf_usage; |
328 |
|
my @subfields; |
329 |
|
|
330 |
# has subfields? |
# has subfields? |
331 |
if ($l =~ m/\x1F/) { |
if ($l =~ m/\x1F/) { |
332 |
foreach my $t (split(/\x1F/,$l)) { |
foreach my $t (split(/\x1F/,$l)) { |
333 |
next if (! $t); |
next if (! $t); |
334 |
my $f = substr($t,0,1); |
my $f = substr($t,0,1); |
335 |
# repeatable subfileds. When we hit first one, |
|
336 |
# store CURRENT (up to that) in first repetition |
push @subfields, ( $f, $sf_usage->{$f}++ || 0 ); |
337 |
# of this record. Then, new record with same |
|
338 |
# identifiers will be created. |
# repeatable subfiled -- convert it to array |
339 |
if ($val->{$f}) { |
if ($val->{$f}) { |
340 |
push @{$rec->{$k}}, $val; |
if ( ref($val->{$f}) ne 'ARRAY' ) { |
341 |
$val = { |
$val->{$f} = [ $val->{$f}, $val ]; |
342 |
i1 => $val->{i1}, |
} else { |
343 |
i2 => $val->{i2}, |
push @{$val->{$f}}, $val; |
344 |
}; |
} |
345 |
} |
} |
346 |
$val->{substr($t,0,1)} = substr($t,1); |
$val->{substr($t,0,1)} = substr($t,1); |
347 |
} |
} |
348 |
|
$val->{subfields} = [ @subfields ] if $args->{include_subfields}; |
349 |
} else { |
} else { |
350 |
$val = $l; |
$val = $l; |
351 |
} |
} |
352 |
|
|
353 |
push @{$rec->{$k}}, $val; |
push @{$rec->{$rec_nr}}, $val; |
354 |
} |
} |
355 |
} |
} |
356 |
|
|
357 |
return $rec; |
return $rec; |
358 |
} |
} |
359 |
|
|
360 |
|
=head2 to_ascii |
361 |
|
|
362 |
|
print $marc->to_ascii( 42 ); |
363 |
|
|
364 |
|
=cut |
365 |
|
|
366 |
|
sub to_ascii { |
367 |
|
my $self = shift; |
368 |
|
|
369 |
|
my $mfn = shift || confess "need mfn"; |
370 |
|
my $row = $self->fetch($mfn) || return; |
371 |
|
|
372 |
|
my $out; |
373 |
|
|
374 |
|
foreach my $f (sort keys %{$row}) { |
375 |
|
my $dump = join('', @{ $row->{$f} }); |
376 |
|
$dump =~ s/\x1e$//; |
377 |
|
$dump =~ s/\x1f/\$/g; |
378 |
|
$out .= "$f\t$dump\n"; |
379 |
|
} |
380 |
|
|
381 |
|
return $out; |
382 |
|
} |
383 |
|
|
384 |
1; |
1; |
385 |
__END__ |
__END__ |
386 |
|
|
387 |
=head1 BUGS |
=head1 UTF-8 ENCODING |
388 |
|
|
389 |
|
This module does nothing with encoding. But, since MARC format is byte |
390 |
|
oriented even when using UTF-8 which has variable number of bytes for each |
391 |
|
character, file is opened in binary mode. |
392 |
|
|
393 |
|
As a result, all scalars recturned to perl don't have utf-8 flag. Solution is |
394 |
|
to use C<hash_filter> and L<Encode> to decode utf-8 encoding like this: |
395 |
|
|
396 |
=head1 SUPPORT |
use Encode; |
397 |
|
|
398 |
|
my $marc = new MARC::Fast( |
399 |
|
marcdb => 'utf8.marc', |
400 |
|
hash_filter => sub { |
401 |
|
Encode::decode( 'utf-8', $_[0] ); |
402 |
|
}, |
403 |
|
); |
404 |
|
|
405 |
|
This will affect C<to_hash>, but C<fetch> will still return binary representation |
406 |
|
since it doesn't support C<hash_filter>. |
407 |
|
|
408 |
=head1 AUTHOR |
=head1 AUTHOR |
409 |
|
|
423 |
|
|
424 |
=head1 SEE ALSO |
=head1 SEE ALSO |
425 |
|
|
426 |
perl(1). |
L<Biblio::Isis>, perl(1). |
427 |
|
|
428 |
=cut |
=cut |