9 |
use File::Slurp; |
use File::Slurp; |
10 |
use Data::Dumper; |
use Data::Dumper; |
11 |
|
|
12 |
|
my $LOOKUP_REGEX = 'lookup{[^\{\}]+}'; |
13 |
|
my $LOOKUP_REGEX_SAVE = 'lookup{([^\{\}]+)}'; |
14 |
|
|
15 |
=head1 NAME |
=head1 NAME |
16 |
|
|
17 |
WebPAC::Lookup - simple normalisation plugin to produce lookup |
WebPAC::Lookup - simple normalisation plugin to produce lookup |
84 |
|
|
85 |
Create lookup from record using lookup definition. |
Create lookup from record using lookup definition. |
86 |
|
|
87 |
$self->create_lookup($rec); |
$self->add($rec); |
88 |
|
|
89 |
Returns true if this record produced lookup. |
Returns true if this record produced lookup. |
90 |
|
|
127 |
return $n; |
return $n; |
128 |
} |
} |
129 |
|
|
130 |
|
=head2 lookup |
131 |
|
|
132 |
|
Perform lookups on format supplied to it. |
133 |
|
|
134 |
|
my $text = $lookup->lookup('[v900]'); |
135 |
|
|
136 |
|
Lookups can be nested (like C<[d:[a:[v900]]]>). |
137 |
|
|
138 |
|
=cut |
139 |
|
|
140 |
|
sub lookup { |
141 |
|
my $self = shift; |
142 |
|
|
143 |
|
my $log = $self->_get_logger(); |
144 |
|
|
145 |
|
my $tmp = shift || $log->logconfess("need format"); |
146 |
|
|
147 |
|
if ($tmp =~ /$LOOKUP_REGEX/o) { |
148 |
|
my @in = ( $tmp ); |
149 |
|
|
150 |
|
$log->debug("lookup for: ",$tmp); |
151 |
|
|
152 |
|
my @out; |
153 |
|
while (my $f = shift @in) { |
154 |
|
if ($f =~ /$LOOKUP_REGEX_SAVE/o) { |
155 |
|
my $k = $1; |
156 |
|
if ($self->{'lookup'}->{$k}) { |
157 |
|
foreach my $nv (@{$self->{'lookup'}->{$k}}) { |
158 |
|
my $tmp2 = $f; |
159 |
|
$tmp2 =~ s/lookup{$k}/$nv/g; |
160 |
|
push @in, $tmp2; |
161 |
|
} |
162 |
|
} else { |
163 |
|
undef $f; |
164 |
|
} |
165 |
|
} elsif ($f) { |
166 |
|
push @out, $f; |
167 |
|
} |
168 |
|
} |
169 |
|
$log->logconfess("return is array and it's not expected!") unless wantarray; |
170 |
|
return @out; |
171 |
|
} else { |
172 |
|
return $tmp; |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
=head1 AUTHOR |
=head1 AUTHOR |
177 |
|
|
178 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |