--- trunk/lib/WebPAC/Input.pm 2007/02/04 13:28:30 797 +++ trunk/lib/WebPAC/Input.pm 2007/06/22 00:03:46 873 @@ -16,11 +16,11 @@ =head1 VERSION -Version 0.17 +Version 0.18 =cut -our $VERSION = '0.17'; +our $VERSION = '0.18'; =head1 SYNOPSIS @@ -64,6 +64,9 @@ encoding => 'ISO-8859-2', recode => 'char pairs', no_progress_bar => 1, + input_config => { + mapping => [ 'foo', 'bar', 'baz' ], + }, ); C is low-level file format module. See L and @@ -233,6 +236,7 @@ my $ll_db = $class->new( path => $arg->{path}, + input_config => $arg->{input_config} || $self->{input_config}, # filter => sub { # my ($l,$f_nr) = @_; # return unless defined($l); @@ -282,10 +286,13 @@ $log->debug("position: $pos\n"); my $rec = $ll_db->fetch_rec($pos, sub { - my ($l,$f_nr) = @_; + my ($l,$f_nr,$debug) = @_; # return unless defined($l); # return $l unless ($rec_regex && $f_nr); + return unless ( defined($l) && defined($f_nr) ); + + warn "-=> $f_nr ## |$l|\n" if ($debug); $log->debug("-=> $f_nr ## $l"); # codepage conversion and recode_regex @@ -298,15 +305,22 @@ my $c = 0; foreach my $r (@{ $rec_regex->{$f_nr} }) { my $old_l = $l; - eval '$l =~ ' . $r; + $log->logconfess("expected regex in ", dump( $r )) unless defined($r->{regex}); + eval '$l =~ ' . $r->{regex}; if ($old_l ne $l) { - $log->debug("REGEX on $f_nr eval \$l =~ $r\n## old l: [$old_l]\n## new l: [$l]"); + my $d = "|$old_l| -> |$l| "; # . $r->{regex}; + $d .= ' +' . $r->{line} . ' ' . $r->{file} if defined($r->{line}); + $d .= ' ' . $r->{debug} if defined($r->{debug}); + $log->debug("MODIFY $d"); + warn "*** $d\n" if ($debug); + } $log->error("error applying regex: $r") if ($@); } } - $log->debug("<=- $f_nr ## $l"); + $log->debug("<=- $f_nr ## |$l|"); + warn "<=- $f_nr ## $l\n" if ($debug); return $l; }); @@ -509,7 +523,8 @@ my $out = join("\n", map { - my $f = $_ || die "no field"; + my $f = $_; + die "no field in ", dump( $s->{fld} ) unless defined( $f ); my $v = $s->{fld}->{$f} || die "no s->{fld}->{$f}"; $max_fld = $v if ($v > $max_fld); @@ -529,7 +544,7 @@ } $o; - } sort { $a cmp $b } keys %{ $s->{fld} } + } sort { $a <=> $b } keys %{ $s->{fld} } ); $log->debug( sub { dump($s) } ); @@ -548,21 +563,22 @@ return unless $self->{ll_db}; - if ($self->{ll_db}->can('dump_rec')) { + if ($self->{ll_db}->can('dump_ascii')) { return $self->{ll_db}->dump_ascii( $self->{pos} ); } else { return dump( $self->{ll_db}->fetch_rec( $self->{pos} ) ); } } -=head2 modify_record_regexps +=head2 _get_regex -Generate hash with regexpes to be applied using l. +Helper function called which create regexps to be execute on code. - my $regexpes = $input->modify_record_regexps( - 900 => { '^a' => { ' : ' => '^b' } }, - 901 => { '*' => { '^b' => ' ; ' } }, - ); + _get_regex( 900, 'regex:[0-9]+' ,'numbers' ); + _get_regex( 900, '^b', ' : ^b' ); + +It supports perl regexps with C prefix to from value and has +additional logic to skip empty subfields. =cut @@ -579,14 +595,30 @@ $from = '\Q' . $from . '\E'; } if ($sf =~ /^\^/) { + my $need_subfield_data = '*'; # no + # if from is also subfield, require some data in between + # to correctly skip empty subfields + $need_subfield_data = '+' if ($from =~ m/^\\Q\^/); return - 's/\Q'. $sf .'\E([^\^]*?)'. $from .'([^\^]*?)/'. $sf .'$1'. $to .'$2/'; + 's/\Q'. $sf .'\E([^\^]' . $need_subfield_data . '?)'. $from .'([^\^]*?)/'. $sf .'$1'. $to .'$2/'; } else { return 's/'. $from .'/'. $to .'/g'; } } + +=head2 modify_record_regexps + +Generate hash with regexpes to be applied using L. + + my $regexpes = $input->modify_record_regexps( + 900 => { '^a' => { ' : ' => '^b' } }, + 901 => { '*' => { '^b' => ' ; ' } }, + ); + +=cut + sub modify_record_regexps { my $self = shift; my $modify_record = {@_}; @@ -604,10 +636,11 @@ foreach my $from (keys %{ $modify_record->{$f}->{$sf} }) { my $to = $modify_record->{$f}->{$sf}->{$from}; #die "no field?" unless defined($to); - $log->debug("transform: |$from| -> |$to|"); + my $d = "|$from| -> |$to|"; + $log->debug("transform: $d"); my $regex = _get_regex($sf,$from,$to); - push @{ $regexpes->{$f} }, $regex; + push @{ $regexpes->{$f} }, { regex => $regex, debug => $d }; $log->debug("regex: $regex"); } } @@ -618,7 +651,7 @@ =head2 modify_file_regexps -Generate hash with regexpes to be applied using l from +Generate hash with regexpes to be applied using L from pseudo hash/yaml format for regex mappings. It should be obvious: @@ -668,7 +701,11 @@ $log->debug("transform: |$from| -> |$to|"); my $regex = _get_regex($sf,$from,$to); - push @{ $regexpes->{$f} }, $regex; + push @{ $regexpes->{$f} }, { + regex => $regex, + file => $modify_path, + line => $., + }; $log->debug("regex: $regex"); } }