From 9754d56ebd6da8c8ded4cc4773362d8644b5c3b9 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Thu, 21 Jun 2007 23:54:42 +0000 Subject: [PATCH] r1294@llin: dpavlin | 2007-06-22 01:54:51 +0200 extract common _to_hash into WebPAC::Input::Helper git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@871 07558da8-63fa-0310-ba24-9fe276d99e06 --- lib/WebPAC/Input/DBF.pm | 77 +------------------------------ lib/WebPAC/Input/Gutenberg.pm | 81 ++------------------------------- lib/WebPAC/Input/Helper.pm | 86 +++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 152 deletions(-) create mode 100644 lib/WebPAC/Input/Helper.pm diff --git a/lib/WebPAC/Input/DBF.pm b/lib/WebPAC/Input/DBF.pm index ecd5098..84506f2 100644 --- a/lib/WebPAC/Input/DBF.pm +++ b/lib/WebPAC/Input/DBF.pm @@ -4,7 +4,8 @@ use warnings; use strict; use WebPAC::Input; -use base qw/WebPAC::Common/; +use WebPAC::Input::Helper; +use base qw/WebPAC::Common WebPAC::Input::Helper/; use XBase; use Data::Dump qw/dump/; use Encode qw/encode_utf8/; @@ -171,80 +172,6 @@ sub size { return $self->{size}; } -=head2 _to_hash - -Return hash from row. Taken from L - - my $rec = $ll_db->_to_hash( - mfn => $mfn, - row => $row, - ); - -=cut - -sub _to_hash { - my $self = shift; - - my $arg = {@_}; - - my $log = $self->_get_logger(); - - my $hash_filter = $arg->{hash_filter}; - my $mfn = $arg->{mfn} || $log->logconfess("need mfn in arguments"); - my $row = $arg->{row} || $log->logconfess("need row in arguments"); - - # init record to include MFN as field 000 - my $rec = { '000' => [ $mfn ] }; - - foreach my $f_nr (keys %{$row}) { - foreach my $l (@{$row->{$f_nr}}) { - - # filter output - $l = $hash_filter->($l, $f_nr) if ($hash_filter); - next unless defined($l); - - my $val; - my $r_sf; # repeatable subfields in this record - - # has subfields? - if ($l =~ m/\^/) { - foreach my $t (split(/\^/,$l)) { - next if (! $t); - my ($sf,$v) = (substr($t,0,1), substr($t,1)); - next unless (defined($v) && $v ne ''); - - if (ref( $val->{$sf} ) eq 'ARRAY') { - - push @{ $val->{$sf} }, $v; - - # record repeatable subfield it it's offset - push @{ $val->{subfields} }, ( $sf, $#{ $val->{$sf} } ); - $r_sf->{$sf}++; - - } elsif (defined( $val->{$sf} )) { - - # convert scalar field to array - $val->{$sf} = [ $val->{$sf}, $v ]; - - push @{ $val->{subfields} }, ( $sf, 1 ); - $r_sf->{$sf}++; - - } else { - $val->{$sf} = $v; - push @{ $val->{subfields} }, ( $sf, 0 ); - } - } - } else { - $val = $l; - } - - push @{$rec->{$f_nr}}, $val; - } - } - - return $rec; -} - =head1 AUTHOR Dobrica Pavlinusic, C<< >> diff --git a/lib/WebPAC/Input/Gutenberg.pm b/lib/WebPAC/Input/Gutenberg.pm index 520cab3..cebd276 100644 --- a/lib/WebPAC/Input/Gutenberg.pm +++ b/lib/WebPAC/Input/Gutenberg.pm @@ -4,7 +4,8 @@ use warnings; use strict; use WebPAC::Input; -use base qw/WebPAC::Common/; +use WebPAC::Input::Helper; +use base qw/WebPAC::Common WebPAC::Input::Helper/; use XML::LibXML; use Data::Dump qw/dump/; use Encode qw/encode_utf8/; @@ -15,11 +16,11 @@ WebPAC::Input::Gutenberg - support for RDF catalog data from Project Gutenberg =head1 VERSION -Version 0.01 +Version 0.02 =cut -our $VERSION = '0.01'; +our $VERSION = '0.02'; =head1 SYNOPSIS @@ -165,80 +166,6 @@ sub size { return $self->{size}; } -=head2 _to_hash - -Return hash from row. Taken from L - - my $rec = $ll_db->_to_hash( - mfn => $mfn; - $row - ); - -=cut - -sub _to_hash { - my $self = shift; - - my $arg = {@_}; - - my $log = $self->_get_logger(); - - my $hash_filter = $arg->{hash_filter}; - my $mfn = $arg->{mfn} || $log->logconfess("need mfn in arguments"); - my $row = $arg->{row} || $log->logconfess("need row in arguments"); - - # init record to include MFN as field 000 - my $rec = { '000' => [ $mfn ] }; - - foreach my $f_nr (keys %{$row}) { - foreach my $l (@{$row->{$f_nr}}) { - - # filter output - $l = $hash_filter->($l, $f_nr) if ($hash_filter); - next unless defined($l); - - my $val; - my $r_sf; # repeatable subfields in this record - - # has subfields? - if ($l =~ m/\^/) { - foreach my $t (split(/\^/,$l)) { - next if (! $t); - my ($sf,$v) = (substr($t,0,1), substr($t,1)); - next unless (defined($v) && $v ne ''); - - if (ref( $val->{$sf} ) eq 'ARRAY') { - - push @{ $val->{$sf} }, $v; - - # record repeatable subfield it it's offset - push @{ $val->{subfields} }, ( $sf, $#{ $val->{$sf} } ); - $r_sf->{$sf}++; - - } elsif (defined( $val->{$sf} )) { - - # convert scalar field to array - $val->{$sf} = [ $val->{$sf}, $v ]; - - push @{ $val->{subfields} }, ( $sf, 1 ); - $r_sf->{$sf}++; - - } else { - $val->{$sf} = $v; - push @{ $val->{subfields} }, ( $sf, 0 ); - } - } - } else { - $val = $l; - } - - push @{$rec->{$f_nr}}, $val; - } - } - - return $rec; -} - =head1 AUTHOR Dobrica Pavlinusic, C<< >> diff --git a/lib/WebPAC/Input/Helper.pm b/lib/WebPAC/Input/Helper.pm new file mode 100644 index 0000000..7e8dfb9 --- /dev/null +++ b/lib/WebPAC/Input/Helper.pm @@ -0,0 +1,86 @@ +package WebPAC::Input::Helper; + +use strict; +use warnings; + +=head1 NAME + +WebPAC::Input::Helper - set of routines to make writing input modules easier + +=cut + +=head2 _to_hash + +Return hash from row. Taken from L + + my $rec = $ll_db->_to_hash( + mfn => $mfn, + row => $row, + ); + +=cut + +sub _to_hash { + my $self = shift; + + my $arg = {@_}; + + my $log = $self->_get_logger(); + + my $hash_filter = $arg->{hash_filter}; + my $mfn = $arg->{mfn} || $log->logconfess("need mfn in arguments"); + my $row = $arg->{row} || $log->logconfess("need row in arguments"); + + # init record to include MFN as field 000 + my $rec = { '000' => [ $mfn ] }; + + foreach my $f_nr (keys %{$row}) { + foreach my $l (@{$row->{$f_nr}}) { + + # filter output + $l = $hash_filter->($l, $f_nr) if ($hash_filter); + next unless defined($l); + + my $val; + my $r_sf; # repeatable subfields in this record + + # has subfields? + if ($l =~ m/\^/) { + foreach my $t (split(/\^/,$l)) { + next if (! $t); + my ($sf,$v) = (substr($t,0,1), substr($t,1)); + next unless (defined($v) && $v ne ''); + + if (ref( $val->{$sf} ) eq 'ARRAY') { + + push @{ $val->{$sf} }, $v; + + # record repeatable subfield it it's offset + push @{ $val->{subfields} }, ( $sf, $#{ $val->{$sf} } ); + $r_sf->{$sf}++; + + } elsif (defined( $val->{$sf} )) { + + # convert scalar field to array + $val->{$sf} = [ $val->{$sf}, $v ]; + + push @{ $val->{subfields} }, ( $sf, 1 ); + $r_sf->{$sf}++; + + } else { + $val->{$sf} = $v; + push @{ $val->{subfields} }, ( $sf, 0 ); + } + } + } else { + $val = $l; + } + + push @{$rec->{$f_nr}}, $val; + } + } + + return $rec; +} + +1; -- 2.20.1