4 |
use Carp; |
use Carp; |
5 |
use File::Glob qw(:globally :nocase); |
use File::Glob qw(:globally :nocase); |
6 |
|
|
|
use Data::Dumper; |
|
|
|
|
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.13; |
$VERSION = 0.22; |
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 (); |
84 |
read_fdt => 1, |
read_fdt => 1, |
85 |
include_deleted => 1, |
include_deleted => 1, |
86 |
hash_filter => sub { |
hash_filter => sub { |
87 |
my $v = shift; |
my ($v,$field_number) = @_; |
88 |
$v =~ s#foo#bar#g; |
$v =~ s#foo#bar#g; |
89 |
}, |
}, |
90 |
debug => 1, |
debug => 1, |
91 |
|
join_subfields_with => ' ; ', |
92 |
); |
); |
93 |
|
|
94 |
Options are described below: |
Options are described below: |
114 |
|
|
115 |
=item hash_filter |
=item hash_filter |
116 |
|
|
117 |
Filter code ref which will be used before data is converted to hash. |
Filter code ref which will be used before data is converted to hash. It will |
118 |
|
receive two arguments, whole line from current field (in C<< $_[0] >>) and |
119 |
|
field number (in C<< $_[1] >>). |
120 |
|
|
121 |
=item debug |
=item debug |
122 |
|
|
123 |
Dump a B<lot> of debugging output. |
Dump a B<lot> of debugging output even at level 1. For even more increase level. |
124 |
|
|
125 |
|
=item join_subfields_with |
126 |
|
|
127 |
|
Define delimiter which will be used to join repeatable subfields. This |
128 |
|
option is included to support lagacy application written against version |
129 |
|
older than 0.21 of this module. By default, it disabled. See L</to_hash>. |
130 |
|
|
131 |
=back |
=back |
132 |
|
|
160 |
} |
} |
161 |
} |
} |
162 |
|
|
163 |
print STDERR "## using files: ",join(" ",@isis_files),"\n" if ($self->{debug}); |
if ($self->{debug}) { |
164 |
|
print STDERR "## using files: ",join(" ",@isis_files),"\n"; |
165 |
|
eval "use Data::Dump"; |
166 |
|
|
167 |
|
if (! $@) { |
168 |
|
*Dumper = *Data::Dump::dump; |
169 |
|
} else { |
170 |
|
use Data::Dumper; |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
# if you want to read .FDT file use read_fdt argument when creating class! |
# if you want to read .FDT file use read_fdt argument when creating class! |
175 |
if ($self->{read_fdt} && -e $self->{fdt_file}) { |
if ($self->{read_fdt} && -e $self->{fdt_file}) { |
218 |
read($self->{'fileMST'}, $buff, 4) || croak "can't read NXTMFN from MST: $!"; |
read($self->{'fileMST'}, $buff, 4) || croak "can't read NXTMFN from MST: $!"; |
219 |
$self->{'NXTMFN'}=unpack("V",$buff) || croak "NXTNFN is zero"; |
$self->{'NXTMFN'}=unpack("V",$buff) || croak "NXTNFN is zero"; |
220 |
|
|
221 |
print STDERR Dumper($self),"\n" if ($self->{debug}); |
print STDERR "## self ",Dumper($self),"\n" if ($self->{debug}); |
222 |
|
|
223 |
# open files for later |
# open files for later |
224 |
open($self->{'fileXRF'}, $self->{xrf_file}) || croak "can't open '$self->{xrf_file}': $!"; |
open($self->{'fileXRF'}, $self->{xrf_file}) || croak "can't open '$self->{xrf_file}': $!"; |
394 |
return $self->{'record'}; |
return $self->{'record'}; |
395 |
} |
} |
396 |
|
|
397 |
|
=head2 mfn |
398 |
|
|
399 |
|
Returns current MFN position |
400 |
|
|
401 |
|
my $mfn = $isis->mfn; |
402 |
|
|
403 |
|
=cut |
404 |
|
|
405 |
|
# This function should be simple return $self->{current_mfn}, |
406 |
|
# but if new is called with _hack_mfn it becomes setter. |
407 |
|
# It's useful in tests when setting $isis->{record} directly |
408 |
|
|
409 |
|
sub mfn { |
410 |
|
my $self = shift; |
411 |
|
return $self->{current_mfn}; |
412 |
|
}; |
413 |
|
|
414 |
|
|
415 |
=head2 to_ascii |
=head2 to_ascii |
416 |
|
|
417 |
Returns ASCII output of record with specified MFN |
Returns ASCII output of record with specified MFN |
491 |
} |
} |
492 |
], |
], |
493 |
|
|
494 |
|
In case there are repeatable subfields in record, this will create |
495 |
|
following structure: |
496 |
|
|
497 |
|
'900' => [ { |
498 |
|
'a' => [ 'foo', 'bar', 'baz' ], |
499 |
|
}] |
500 |
|
|
501 |
|
Or in more complex example of |
502 |
|
|
503 |
|
902 ^aa1^aa2^aa3^bb1^aa4^bb2^cc1^aa5 |
504 |
|
|
505 |
|
it will create |
506 |
|
|
507 |
|
902 => [ |
508 |
|
{ a => ["a1", "a2", "a3", "a4", "a5"], b => ["b1", "b2"], c => "c1" }, |
509 |
|
], |
510 |
|
|
511 |
|
This behaviour can be changed using C<join_subfields_with> option to L</new>, |
512 |
|
in which case C<to_hash> will always create single value for each subfield. |
513 |
|
This will change result to: |
514 |
|
|
515 |
|
|
516 |
|
|
517 |
This method will also create additional field C<000> with MFN. |
This method will also create additional field C<000> with MFN. |
518 |
|
|
519 |
|
There is also more elaborative way to call C<to_hash> like this: |
520 |
|
|
521 |
|
my $hash = $isis->to_hash({ |
522 |
|
mfn => 42, |
523 |
|
include_subfields => 1, |
524 |
|
}); |
525 |
|
|
526 |
|
Each option controll creation of hash: |
527 |
|
|
528 |
|
=over 4 |
529 |
|
|
530 |
|
=item mfn |
531 |
|
|
532 |
|
Specify MFN number of record |
533 |
|
|
534 |
|
=item include_subfields |
535 |
|
|
536 |
|
This option will create additional key in hash called C<subfields> which will |
537 |
|
have original record subfield order and index to that subfield like this: |
538 |
|
|
539 |
|
902 => [ { |
540 |
|
a => ["a1", "a2", "a3", "a4", "a5"], |
541 |
|
b => ["b1", "b2"], |
542 |
|
c => "c1", |
543 |
|
subfields => ["a", 0, "a", 1, "a", 2, "b", 0, "a", 3, "b", 1, "c", 0, "a", 4], |
544 |
|
} ], |
545 |
|
|
546 |
|
=item join_subfields_with |
547 |
|
|
548 |
|
Define delimiter which will be used to join repeatable subfields. You can |
549 |
|
specify option here instead in L</new> if you want to have per-record control. |
550 |
|
|
551 |
|
=back |
552 |
|
|
553 |
=cut |
=cut |
554 |
|
|
555 |
sub to_hash { |
sub to_hash { |
556 |
my $self = shift; |
my $self = shift; |
557 |
|
|
558 |
|
|
559 |
my $mfn = shift || confess "need mfn!"; |
my $mfn = shift || confess "need mfn!"; |
560 |
|
my $arg; |
561 |
|
|
562 |
|
if (ref($mfn) eq 'HASH') { |
563 |
|
$arg = $mfn; |
564 |
|
$mfn = $arg->{mfn} || confess "need mfn in arguments"; |
565 |
|
} |
566 |
|
|
567 |
# init record to include MFN as field 000 |
# init record to include MFN as field 000 |
568 |
my $rec = { '000' => [ $mfn ] }; |
my $rec = { '000' => [ $mfn ] }; |
569 |
|
|
570 |
my $row = $self->fetch($mfn) || return; |
my $row = $self->fetch($mfn) || return; |
571 |
|
|
572 |
foreach my $k (keys %{$row}) { |
my $j_rs = $arg->{join_subfields_with}; |
573 |
foreach my $l (@{$row->{$k}}) { |
$j_rs = $self->{join_subfields_with} unless(defined($j_rs)); |
574 |
|
my $i_sf = $arg->{include_subfields}; |
575 |
|
|
576 |
|
foreach my $f_nr (keys %{$row}) { |
577 |
|
foreach my $l (@{$row->{$f_nr}}) { |
578 |
|
|
579 |
# filter output |
# filter output |
580 |
$l = $self->{'hash_filter'}->($l) if ($self->{'hash_filter'}); |
if ($self->{'hash_filter'}) { |
581 |
|
$l = $self->{'hash_filter'}->($l, $f_nr); |
582 |
|
next unless defined($l); |
583 |
|
} |
584 |
|
|
585 |
my $val; |
my $val; |
586 |
|
my $r_sf; # repeatable subfields in this record |
587 |
|
|
588 |
# has identifiers? |
# has identifiers? |
589 |
($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\^/\^/); |
($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\^/\^/); |
592 |
if ($l =~ m/\^/) { |
if ($l =~ m/\^/) { |
593 |
foreach my $t (split(/\^/,$l)) { |
foreach my $t (split(/\^/,$l)) { |
594 |
next if (! $t); |
next if (! $t); |
595 |
$val->{substr($t,0,1)} = substr($t,1); |
my ($sf,$v) = (substr($t,0,1), substr($t,1)); |
596 |
|
# XXX this might be option, but why? |
597 |
|
next unless ($v); |
598 |
|
# warn "### $f_nr^$sf:$v",$/ if ($self->{debug} > 1); |
599 |
|
|
600 |
|
if (ref( $val->{$sf} ) eq 'ARRAY') { |
601 |
|
|
602 |
|
push @{ $val->{$sf} }, $v; |
603 |
|
|
604 |
|
# record repeatable subfield it it's offset |
605 |
|
push @{ $val->{subfields} }, ( $sf, $#{ $val->{$sf} } ) if (! $j_rs && $i_sf); |
606 |
|
$r_sf->{$sf}++; |
607 |
|
|
608 |
|
} elsif (defined( $val->{$sf} )) { |
609 |
|
|
610 |
|
# convert scalar field to array |
611 |
|
$val->{$sf} = [ $val->{$sf}, $v ]; |
612 |
|
|
613 |
|
push @{ $val->{subfields} }, ( $sf, 1 ) if (! $j_rs && $i_sf); |
614 |
|
$r_sf->{$sf}++; |
615 |
|
|
616 |
|
} else { |
617 |
|
$val->{$sf} = $v; |
618 |
|
push @{ $val->{subfields} }, ( $sf, 0 ) if ($i_sf); |
619 |
|
} |
620 |
} |
} |
621 |
} else { |
} else { |
622 |
$val = $l; |
$val = $l; |
623 |
} |
} |
624 |
|
|
625 |
push @{$rec->{$k}}, $val; |
if ($j_rs) { |
626 |
|
map { |
627 |
|
$val->{$_} = join($j_rs, @{ $val->{$_} }); |
628 |
|
} keys %$r_sf |
629 |
|
} |
630 |
|
|
631 |
|
push @{$rec->{$f_nr}}, $val; |
632 |
} |
} |
633 |
} |
} |
634 |
|
|
741 |
tested this against ouput of one C<isis.dll>-based application, but I don't |
tested this against ouput of one C<isis.dll>-based application, but I don't |
742 |
know any details about it's version. |
know any details about it's version. |
743 |
|
|
744 |
|
=head1 VERSIONS |
745 |
|
|
746 |
|
As this is young module, new features are added in subsequent version. It's |
747 |
|
a good idea to specify version when using this module like this: |
748 |
|
|
749 |
|
use Biblio::Isis 0.21 |
750 |
|
|
751 |
|
Below is list of changes in specific version of module (so you can target |
752 |
|
older versions if you really have to): |
753 |
|
|
754 |
|
=over 8 |
755 |
|
|
756 |
|
=item 0.22 |
757 |
|
|
758 |
|
Added field number when calling C<hash_filter> |
759 |
|
|
760 |
|
=item 0.21 |
761 |
|
|
762 |
|
Added C<join_subfields_with> to L</new> and L</to_hash>. |
763 |
|
|
764 |
|
Added C<include_subfields> to L</to_hash>. |
765 |
|
|
766 |
|
=item 0.20 |
767 |
|
|
768 |
|
Added C<< $isis->mfn >>, support for repeatable subfields and |
769 |
|
C<< $isis->to_hash({ mfn => 42, ... }) >> calling convention |
770 |
|
|
771 |
|
=back |
772 |
|
|
773 |
=head1 AUTHOR |
=head1 AUTHOR |
774 |
|
|
775 |
Dobrica Pavlinusic |
Dobrica Pavlinusic |
791 |
|
|
792 |
=head1 SEE ALSO |
=head1 SEE ALSO |
793 |
|
|
794 |
|
L<Biblio::Isis::Manual> for CDS/ISIS manual appendix F, G and H which describe file format |
795 |
|
|
796 |
OpenIsis web site L<http://www.openisis.org> |
OpenIsis web site L<http://www.openisis.org> |
797 |
|
|
798 |
perl4lib site L<http://perl4lib.perl.org> |
perl4lib site L<http://perl4lib.perl.org> |