16 |
|
|
17 |
=head1 VERSION |
=head1 VERSION |
18 |
|
|
19 |
Version 0.12 |
Version 0.13 |
20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
our $VERSION = '0.12'; |
our $VERSION = '0.13'; |
24 |
|
|
25 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
26 |
|
|
98 |
$log->logconfess("lookup argument is not suppored any more. rewrite call to lookup_ref") if ($self->{lookup}); |
$log->logconfess("lookup argument is not suppored any more. rewrite call to lookup_ref") if ($self->{lookup}); |
99 |
|
|
100 |
$log->logconfess("specify low-level file format module") unless ($self->{module}); |
$log->logconfess("specify low-level file format module") unless ($self->{module}); |
101 |
my $module = $self->{module}; |
my $module_path = $self->{module}; |
102 |
$module =~ s#::#/#g; |
$module_path =~ s#::#/#g; |
103 |
$module .= '.pm'; |
$module_path .= '.pm'; |
104 |
$log->debug("require low-level module $self->{module} from $module"); |
$log->debug("require low-level module $self->{module} from $module_path"); |
105 |
|
|
106 |
require $module; |
require $module_path; |
|
#eval $self->{module} .'->import'; |
|
107 |
|
|
108 |
# check if required subclasses are implemented |
# check if required subclasses are implemented |
109 |
foreach my $subclass (qw/open_db fetch_rec init/) { |
foreach my $subclass (qw/open_db fetch_rec init dump_rec/) { |
110 |
my $n = $self->{module} . '::' . $subclass; |
# FIXME |
|
if (! defined &{ $n }) { |
|
|
my $missing = "missing $subclass in $self->{module}"; |
|
|
$self->{$subclass} = sub { $log->logwarn($missing) }; |
|
|
} else { |
|
|
$self->{$subclass} = \&{ $n }; |
|
|
} |
|
|
} |
|
|
|
|
|
if ($self->{init}) { |
|
|
$log->debug("calling init"); |
|
|
$self->{init}->($self, @_); |
|
111 |
} |
} |
112 |
|
|
113 |
$self->{'encoding'} ||= 'ISO-8859-2'; |
$self->{'encoding'} ||= 'ISO-8859-2'; |
150 |
code_page => 'cp852', |
code_page => 'cp852', |
151 |
limit => 500, |
limit => 500, |
152 |
offset => 6000, |
offset => 6000, |
|
lookup => $lookup_obj, |
|
153 |
stats => 1, |
stats => 1, |
154 |
lookup_ref => sub { |
lookup_coderef => sub { |
155 |
my ($k,$v) = @_; |
my $rec = shift; |
156 |
# store lookup $k => $v |
# store lookups |
157 |
}, |
}, |
158 |
modify_records => { |
modify_records => { |
159 |
900 => { '^a' => { ' : ' => '^b' } }, |
900 => { '^a' => { ' : ' => '^b' } }, |
170 |
|
|
171 |
C<stats> create optional report about usage of fields and subfields |
C<stats> create optional report about usage of fields and subfields |
172 |
|
|
173 |
C<lookup_coderef> is closure to call when adding C<< key => 'value' >> combinations to |
C<lookup_coderef> is closure to called to save data into lookups |
|
lookup. |
|
174 |
|
|
175 |
C<modify_records> specify mapping from subfields to delimiters or from |
C<modify_records> specify mapping from subfields to delimiters or from |
176 |
delimiters to subfields, as well as oprations on fields (if subfield is |
delimiters to subfields, as well as oprations on fields (if subfield is |
195 |
$log->logconfess("lookup_coderef must be CODE, not ",ref($arg->{lookup_coderef})) |
$log->logconfess("lookup_coderef must be CODE, not ",ref($arg->{lookup_coderef})) |
196 |
if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE'); |
if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE'); |
197 |
|
|
198 |
|
$log->debug( $arg->{lookup_coderef} ? '' : 'not ', "using lookup_coderef"); |
199 |
|
|
200 |
$log->logcroak("need path") if (! $arg->{'path'}); |
$log->logcroak("need path") if (! $arg->{'path'}); |
201 |
my $code_page = $arg->{'code_page'} || 'cp852'; |
my $code_page = $arg->{'code_page'} || 'cp852'; |
202 |
|
|
238 |
} |
} |
239 |
$log->debug("rec_regex: ", Dumper($rec_regex)) if ($rec_regex); |
$log->debug("rec_regex: ", Dumper($rec_regex)) if ($rec_regex); |
240 |
|
|
241 |
my ($db, $size) = $self->{open_db}->( $self, |
my $class = $self->{module} || $log->logconfess("can't get low-level module name!"); |
242 |
|
|
243 |
|
my $ll_db = $class->new( |
244 |
path => $arg->{path}, |
path => $arg->{path}, |
245 |
# filter => sub { |
# filter => sub { |
246 |
# my ($l,$f_nr) = @_; |
# my ($l,$f_nr) = @_; |
252 |
%{ $arg }, |
%{ $arg }, |
253 |
); |
); |
254 |
|
|
255 |
unless (defined($db)) { |
unless (defined($ll_db)) { |
256 |
$log->logwarn("can't open database $arg->{path}, skipping..."); |
$log->logwarn("can't open database $arg->{path}, skipping..."); |
257 |
return; |
return; |
258 |
} |
} |
259 |
|
|
260 |
|
my $size = $ll_db->size; |
261 |
|
|
262 |
unless ($size) { |
unless ($size) { |
263 |
$log->logwarn("no records in database $arg->{path}, skipping..."); |
$log->logwarn("no records in database $arg->{path}, skipping..."); |
264 |
return; |
return; |
290 |
|
|
291 |
$log->debug("position: $pos\n"); |
$log->debug("position: $pos\n"); |
292 |
|
|
293 |
my $rec = $self->{fetch_rec}->($self, $db, $pos, sub { |
my $rec = $ll_db->fetch_rec($pos, sub { |
294 |
my ($l,$f_nr) = @_; |
my ($l,$f_nr) = @_; |
295 |
# return unless defined($l); |
# return unless defined($l); |
296 |
# return $l unless ($rec_regex && $f_nr); |
# return $l unless ($rec_regex && $f_nr); |
340 |
if ($self->{stats}) { |
if ($self->{stats}) { |
341 |
|
|
342 |
# fetch clean record with regexpes applied for statistics |
# fetch clean record with regexpes applied for statistics |
343 |
my $rec = $self->{fetch_rec}->($self, $db, $pos); |
my $rec = $ll_db->fetch_rec($pos); |
344 |
|
|
345 |
foreach my $fld (keys %{ $rec }) { |
foreach my $fld (keys %{ $rec }) { |
346 |
$self->{_stats}->{fld}->{ $fld }++; |
$self->{_stats}->{fld}->{ $fld }++; |
538 |
return $out; |
return $out; |
539 |
} |
} |
540 |
|
|
541 |
|
=head2 dump |
542 |
|
|
543 |
|
Display humanly readable dump of record |
544 |
|
|
545 |
|
=cut |
546 |
|
|
547 |
|
sub dump { |
548 |
|
my $self = shift; |
549 |
|
|
550 |
|
return $self->{dump_rec}->($self, $self->{pos}); |
551 |
|
|
552 |
|
} |
553 |
|
|
554 |
=head2 modify_record_regexps |
=head2 modify_record_regexps |
555 |
|
|
556 |
Generate hash with regexpes to be applied using l<filter>. |
Generate hash with regexpes to be applied using l<filter>. |
632 |
|
|
633 |
my $regexpes; |
my $regexpes; |
634 |
|
|
635 |
CORE::open(my $fh, $modify_path) || $log->die("can't open modify file $modify_path: $!"); |
CORE::open(my $fh, $modify_path) || $log->logdie("can't open modify file $modify_path: $!"); |
636 |
|
|
637 |
my ($f,$sf); |
my ($f,$sf); |
638 |
|
|