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.06; |
$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 |
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); |
154 |
|
|
155 |
my $hash = $marc->fetch(42); |
my $hash = $marc->fetch(42); |
156 |
|
|
157 |
|
First record number is C<1> |
158 |
|
|
159 |
=cut |
=cut |
160 |
|
|
161 |
sub fetch { |
sub fetch { |
162 |
my $self = shift; |
my $self = shift; |
163 |
|
|
164 |
my $rec_nr = shift || return; |
my $rec_nr = shift; |
165 |
|
|
166 |
my $leader = $self->{leaders}->[$rec_nr - 1]; |
if ( ! $rec_nr ) { |
167 |
|
$self->{last_leader} = undef; |
168 |
|
return; |
169 |
|
} |
170 |
|
|
171 |
|
my $leader = $self->{leader}->[$rec_nr - 1]; |
172 |
|
$self->{last_leader} = $leader; |
173 |
unless ($leader) { |
unless ($leader) { |
174 |
carp "can't find record $rec_nr"; |
carp "can't find record $rec_nr"; |
175 |
return; |
return; |
251 |
} |
} |
252 |
|
|
253 |
|
|
254 |
|
=head2 last_leader |
255 |
|
|
256 |
|
Returns leader of last record L<fetch>ed |
257 |
|
|
258 |
|
print $marc->last_leader; |
259 |
|
|
260 |
|
Added in version 0.08 of this module, so if you need it use: |
261 |
|
|
262 |
|
use MARC::Fast 0.08; |
263 |
|
|
264 |
|
to be sure that it's supported. |
265 |
|
|
266 |
|
=cut |
267 |
|
|
268 |
|
sub last_leader { |
269 |
|
my $self = shift; |
270 |
|
return $self->{last_leader}; |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
=head2 to_hash |
=head2 to_hash |
275 |
|
|
276 |
Read record with specified MFN and convert it to hash |
Read record with specified MFN and convert it to hash |
277 |
|
|
278 |
my $hash = $marc->to_hash($mfn); |
my $hash = $marc->to_hash( $mfn, include_subfields => 1, ); |
279 |
|
|
280 |
It has ability to convert characters (using C<hash_filter>) from MARC |
It has ability to convert characters (using C<hash_filter>) from MARC |
281 |
database before creating structures enabling character re-mapping or quick |
database before creating structures enabling character re-mapping or quick |
302 |
|
|
303 |
my $mfn = shift || confess "need mfn!"; |
my $mfn = shift || confess "need mfn!"; |
304 |
|
|
305 |
|
my $args = {@_}; |
306 |
|
|
307 |
# init record to include MFN as field 000 |
# init record to include MFN as field 000 |
308 |
my $rec = { '000' => [ $mfn ] }; |
my $rec = { '000' => [ $mfn ] }; |
309 |
|
|
323 |
# has identifiers? |
# has identifiers? |
324 |
($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/); |
325 |
|
|
326 |
|
my $sf_usage; |
327 |
|
my @subfields; |
328 |
|
|
329 |
# has subfields? |
# has subfields? |
330 |
if ($l =~ m/\x1F/) { |
if ($l =~ m/\x1F/) { |
331 |
foreach my $t (split(/\x1F/,$l)) { |
foreach my $t (split(/\x1F/,$l)) { |
332 |
next if (! $t); |
next if (! $t); |
333 |
my $f = substr($t,0,1); |
my $f = substr($t,0,1); |
334 |
# repeatable subfileds. When we hit first one, |
|
335 |
# store CURRENT (up to that) in first repetition |
push @subfields, ( $f, $sf_usage->{$f}++ || 0 ); |
336 |
# of this record. Then, new record with same |
|
337 |
# identifiers will be created. |
# repeatable subfiled -- convert it to array |
338 |
if ($val->{$f}) { |
if ($val->{$f}) { |
339 |
push @{$rec->{$rec_nr}}, $val; |
if ( $sf_usage->{$f} == 2 ) { |
340 |
$val = { |
$val->{$f} = [ $val->{$f}, $val ]; |
341 |
i1 => $val->{i1}, |
} else { |
342 |
i2 => $val->{i2}, |
push @{$val->{$f}}, $val; |
343 |
}; |
} |
344 |
} |
} |
345 |
$val->{substr($t,0,1)} = substr($t,1); |
$val->{substr($t,0,1)} = substr($t,1); |
346 |
} |
} |
347 |
|
$val->{subfields} = [ @subfields ] if $args->{include_subfields}; |
348 |
} else { |
} else { |
349 |
$val = $l; |
$val = $l; |
350 |
} |
} |