/[webpac2]/trunk/lib/WebPAC/Normalize/Set.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

Contents of /trunk/lib/WebPAC/Normalize/Set.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 475 - (show annotations)
Sat May 13 12:37:25 2006 UTC (17 years, 11 months ago) by dpavlin
File size: 4241 byte(s)
 r610@llin:  dpavlin | 2006-05-13 14:38:42 +0200
 permit setup of empty lookup

1 package WebPAC::Normalize::Set;
2 use Exporter 'import';
3 @EXPORT = qw/
4 set_rec set_lookup
5 get_ds clean_ds
6 tag search display
7 rec1 rec2 rec
8 regex prefix
9 first lookup join_with
10 /;
11
12 use warnings;
13 use strict;
14
15 #use base qw/WebPAC::Common/;
16 use Data::Dumper;
17
18 =head1 NAME
19
20 WebPAC::Normalize::Set - describe normalisaton rules using sets
21
22 =head1 VERSION
23
24 Version 0.03
25
26 =cut
27
28 our $VERSION = '0.03';
29
30 =head1 SYNOPSIS
31
32 This module uses C<conf/normalize/*.pl> files to perform normalisation
33 from input records using perl functions which are specialized for set
34 processing.
35
36 Sets are implemented as arrays, and normalisation file is valid perl, which
37 means that you check it's validity before running WebPAC using
38 C<perl -c normalize.pl>.
39
40 Normalisation can generate multiple output normalized data. For now, supported output
41 types (on the left side of definition) are: C<tag>, C<display> and C<search>.
42
43 =head1 FUNCTIONS
44
45 =head2 set_rec
46
47 Set current record hash
48
49 set_rec( $rec );
50
51 =cut
52
53 my $rec;
54
55 sub set_rec {
56 $rec = shift or die "no record hash";
57 }
58
59 =head2 tag
60
61 Define new tag for I<search> and I<display>.
62
63 tag('Title', rec('200','a') );
64
65
66 =cut
67
68 my $out;
69
70 sub tag {
71 my $name = shift or die "tag needs name as first argument";
72 my @o = grep { defined($_) && $_ ne '' } @_;
73 return unless (@o);
74 $out->{$name}->{tag} = $name;
75 $out->{$name}->{search} = \@o;
76 $out->{$name}->{display} = \@o;
77 }
78
79 =head2 display
80
81 Define tag just for I<display>
82
83 @v = display('Title', rec('200','a') );
84
85 =cut
86
87 sub display {
88 my $name = shift or die "display needs name as first argument";
89 my @o = grep { defined($_) && $_ ne '' } @_;
90 return unless (@o);
91 $out->{$name}->{tag} = $name;
92 $out->{$name}->{display} = \@o;
93 }
94
95 =head2 search
96
97 Prepare values just for I<search>
98
99 @v = search('Title', rec('200','a') );
100
101 =cut
102
103 sub search {
104 my $name = shift or die "search needs name as first argument";
105 my @o = grep { defined($_) && $_ ne '' } @_;
106 return unless (@o);
107 $out->{$name}->{tag} = $name;
108 $out->{$name}->{search} = \@o;
109 }
110
111 =head2 get_ds
112
113 Return hash formatted as data structure
114
115 my $ds = get_ds();
116
117 =cut
118
119 sub get_ds {
120 return $out;
121 }
122
123 =head2 clean_ds
124
125 Clean data structure hash for next record
126
127 clean_ds();
128
129 =cut
130
131 sub clean_ds {
132 $out = undef;
133 }
134
135 =head2 set_lookup
136
137 Set current lookup hash
138
139 set_lookup( $lookup );
140
141 =cut
142
143 my $lookup;
144
145 sub set_lookup {
146 $lookup = shift;
147 }
148
149 =head2 rec1
150
151 Return all values in some field
152
153 @v = rec1('200')
154
155 TODO: order of values is probably same as in source data, need to investigate that
156
157 =cut
158
159 sub rec1 {
160 my $f = shift;
161 return unless (defined($rec && $rec->{$f}));
162 if (ref($rec->{$f}) eq 'ARRAY') {
163 if (ref($rec->{$f}->[0]) eq 'HASH') {
164 return map { values %{$_} } @{ $rec->{$f} };
165 } else {
166 return @{ $rec->{$f} };
167 }
168 } elsif( defined($rec->{$f}) ) {
169 return $rec->{$f};
170 }
171 }
172
173 =head2 rec2
174
175 Return all values in specific field and subfield
176
177 @v = rec2('200','a')
178
179 =cut
180
181 sub rec2 {
182 my $f = shift;
183 return unless (defined($rec && $rec->{$f}));
184 my $sf = shift;
185 return map { $_->{$sf} } grep { $_->{$sf} } @{ $rec->{$f} };
186 }
187
188 =head2 rec
189
190 syntaxtic sugar for
191
192 @v = rec('200')
193 @v = rec('200','a')
194
195 =cut
196
197 sub rec {
198 if ($#_ == 0) {
199 return rec1(@_);
200 } elsif ($#_ == 1) {
201 return rec2(@_);
202 }
203 }
204
205 =head2 regex
206
207 Apply regex to some or all values
208
209 @v = regex( 's/foo/bar/g', @v );
210
211 =cut
212
213 sub regex {
214 my $r = shift;
215 my @out;
216 #warn "r: $r\n",Dumper(\@_);
217 foreach my $t (@_) {
218 eval "\$t =~ $r";
219 push @out, $t;
220 }
221 return @out;
222 }
223
224 =head2 prefix
225
226 Prefix all values with a string
227
228 @v = prefix( 'my_', @v );
229
230 =cut
231
232 sub prefix {
233 my $p = shift or die "prefix needs string as first argument";
234 return map { $p . $_ } grep { defined($_) } @_;
235 }
236
237 =head2 first
238
239 Return first element
240
241 $v = first( @v );
242
243 =cut
244
245 sub first {
246 my $r = shift;
247 return $r;
248 }
249
250 =head2 lookup
251
252 Consult lookup hashes for some value
253
254 @v = lookup( $v );
255 @v = lookup( @v );
256
257 =cut
258
259 sub lookup {
260 my $k = shift or return;
261 return unless (defined($lookup->{$k}));
262 if (ref($lookup->{$k}) eq 'ARRAY') {
263 return @{ $lookup->{$k} };
264 } else {
265 return $lookup->{$k};
266 }
267 }
268
269 =head2 join_with
270
271 Joins walues with some delimiter
272
273 $v = join_with(", ", @v);
274
275 =cut
276
277 sub join_with {
278 my $d = shift;
279 return join($d, @_);
280 }
281
282 # END
283 1;

  ViewVC Help
Powered by ViewVC 1.1.26