/[webpac2]/trunk/lib/WebPAC/Lookup.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Lookup.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 7 by dpavlin, Sat Jul 16 16:00:19 2005 UTC revision 234 by dpavlin, Tue Dec 6 19:41:17 2005 UTC
# Line 3  package WebPAC::Lookup; Line 3  package WebPAC::Lookup;
3  use warnings;  use warnings;
4  use strict;  use strict;
5    
6  use WebPAC::Common;  use base qw/WebPAC::Common WebPAC::Normalize/;
   
 use base qw/WebPAC::Common/;  
7  use File::Slurp;  use File::Slurp;
8    use YAML qw/LoadFile/;
9  use Data::Dumper;  use Data::Dumper;
10    
 my $LOOKUP_REGEX = 'lookup{[^\{\}]+}';  
 my $LOOKUP_REGEX_SAVE = 'lookup{([^\{\}]+)}';  
   
11  =head1 NAME  =head1 NAME
12    
13  WebPAC::Lookup - simple normalisation plugin to produce lookup  WebPAC::Lookup - simple normalisation plugin to produce lookup
14    
15  =head1 VERSION  =head1 VERSION
16    
17  Version 0.01  Version 0.02
18    
19  =cut  =cut
20    
21  our $VERSION = '0.01';  our $VERSION = '0.02';
22    
23  =head1 SYNOPSIS  =head1 SYNOPSIS
24    
# Line 45  value in lookup. Line 41  value in lookup.
41      'val' => 'v900' },      'val' => 'v900' },
42   ];   ];
43    
44    Just for a reference, lookup data is internally stored in
45    C<< $self->{'_lookup_data'} >>.
46    
47  =head1 FUNCTIONS  =head1 FUNCTIONS
48    
# Line 54  Create new lookup object. Line 52  Create new lookup object.
52    
53    my $lookup = new WebPAC::Lookup(    my $lookup = new WebPAC::Lookup(
54          lookup_file => '/path/to/conf/lookup/lookup.pm',          lookup_file => '/path/to/conf/lookup/lookup.pm',
55            is_lookup_regex => 'lookup{[^\{\}]+}';
56            save_lookup_regex => 'lookup{([^\{\}]+)}';
57    );    );
58    
59  =cut  =cut
# Line 69  sub new { Line 69  sub new {
69    
70          my $lookup_code = read_file($lookup_file) || $log->logconfess("can't read lookup file $lookup_file: $!");          my $lookup_code = read_file($lookup_file) || $log->logconfess("can't read lookup file $lookup_file: $!");
71    
72          {          if ($lookup_file =~ m#\.pm$#) {
73                  no strict 'vars';                  no strict 'vars';
74                  do $lookup_file or $log->logdie("Failed to read configuration parameters '$lookup_file' $! $@");                  do $lookup_file or $log->logdie("Failed to read configuration parameters '$lookup_file' $! $@");
75                  $self->{'lookup_def'} = \@lookup || $log->logdie("lookup config $lookup_file doesn't produce \@lookup array");                  $self->{'lookup_def'} = \@lookup || $log->logdie("lookup config $lookup_file doesn't produce \@lookup array");
76            } elsif ($lookup_file =~ m#\.(:?yml|yaml)$#) {
77                    my $yaml = LoadFile( $lookup_file ) || $log->logdie("lookup YAML file $lookup_file error: $!");
78                    $self->{'lookup_def'} = $yaml->{lookup} || $log->logdie("lookup YAML file $lookup_file should begin with 'lookup:'");
79            } else {
80                    $log->logide("unsupported lookup file $lookup_file");
81          }          }
82    
83          $log->logconfess("lookup config file isn't ARRAY", sub { Dumper( $self->{'lookup_def'} ) }) if ($self->{'lookup_def'} !~ /ARRAY/o);          $log->logconfess("lookup config file isn't ARRAY but ", sub { Dumper( $self->{'lookup_def'} ) }) if ($self->{'lookup_def'} !~ /ARRAY/o);
84    
85            $self->{'is_lookup_regex'} ||= 'lookup{[^\{\}]+}';
86            $self->{'save_lookup_regex'} ||= 'lookup{([^\{\}]+)}';
87    
88    
89            $self->{'LOOKUP_REGEX'} = qr/$self->{'is_lookup_regex'}/;
90            $self->{'LOOKUP_REGEX_SAVE'} = qr/$self->{'save_lookup_regex'}/;
91    
92            $log->debug("regexps: ", $self->{'LOOKUP_REGEX'}, " ", $self->{'LOOKUP_REGEX_SAVE'});
93    
94          $self ? return $self : return undef;          $self ? return $self : return undef;
95  }  }
# Line 114  sub add($) { Line 128  sub add($) {
128                                  my $key = $self->fill_in($rec,$i->{'key'}) || next;                                  my $key = $self->fill_in($rec,$i->{'key'}) || next;
129                                  my @val = $self->fill_in($rec,$i->{'val'}) || next;                                  my @val = $self->fill_in($rec,$i->{'val'}) || next;
130                                  $log->debug("stored $key = ",sub { join(" | ",@val) });                                  $log->debug("stored $key = ",sub { join(" | ",@val) });
131                                  push @{$self->{'lookup'}->{$key}}, @val;                                  push @{$self->{'_lookup_data'}->{$key}}, @val;
132                          }                          }
133                  } else {                  } else {
134                          my $key = $self->fill_in($rec,$i->{'key'}) || next;                          my $key = $self->fill_in($rec,$i->{'key'}) || next;
135                          my @val = $self->fill_in($rec,$i->{'val'}) || next;                          my @val = $self->fill_in($rec,$i->{'val'}) || next;
136                          $log->debug("stored $key = ",sub { join(" | ",@val) });                          $log->debug("stored $key = ",sub { join(" | ",@val) });
137                          push @{$self->{'lookup'}->{$key}}, @val;                          push @{$self->{'_lookup_data'}->{$key}}, @val;
138                  }                  }
139          }          }
140    
# Line 144  sub lookup { Line 158  sub lookup {
158    
159          my $tmp = shift || $log->logconfess("need format");          my $tmp = shift || $log->logconfess("need format");
160    
161          if ($tmp =~ /$LOOKUP_REGEX/o) {          if ($tmp =~ $self->{'LOOKUP_REGEX'}) {
162                  my @in = ( $tmp );                  my @in = ( $tmp );
163    
164                  $log->debug("lookup for: ",$tmp);                  $log->debug("lookup for: ",$tmp);
165    
166                  my @out;                  my @out;
167                  while (my $f = shift @in) {                  while (my $f = shift @in) {
168                          if ($f =~ /$LOOKUP_REGEX_SAVE/o) {                          if ($f =~ $self->{'LOOKUP_REGEX_SAVE'}) {
169                                  my $k = $1;                                  my $k = $1;
170                                  if ($self->{'lookup'}->{$k}) {                                  if ($self->{'_lookup_data'}->{$k}) {
171                                          foreach my $nv (@{$self->{'lookup'}->{$k}}) {                                          foreach my $nv (@{$self->{'_lookup_data'}->{$k}}) {
172                                                  my $tmp2 = $f;                                                  my $tmp2 = $f;
173                                                  $tmp2 =~ s/lookup{$k}/$nv/g;                                                  $tmp2 =~ s/lookup{$k}/$nv/g;
174                                                  push @in, $tmp2;                                                  push @in, $tmp2;
# Line 173  sub lookup { Line 187  sub lookup {
187          }          }
188  }  }
189    
190    =head2 regex
191    
192    Returns precompiled regex for lookup format.
193    
194     if ($foo =~ $lookup->reges) { ... }
195    
196    =cut
197    
198    sub regex {
199            my $self = shift;
200    
201            return $self->{'LOOKUP_REGEX'};
202    }
203    
204  =head1 AUTHOR  =head1 AUTHOR
205    
206  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

Legend:
Removed from v.7  
changed lines
  Added in v.234

  ViewVC Help
Powered by ViewVC 1.1.26