/[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 465 - (show annotations)
Fri May 12 21:46:28 2006 UTC (17 years, 11 months ago) by dpavlin
File size: 3752 byte(s)
 r589@llin:  dpavlin | 2006-05-12 23:49:27 +0200
 lookup fix, remove warn in regex

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
34
35 =head1 FUNCTIONS
36
37 =head2 set_rec
38
39 Set current record hash
40
41 set_rec( $rec );
42
43 =cut
44
45 my $rec;
46
47 sub set_rec {
48 $rec = shift or die "no record hash";
49 }
50
51 =head2 tag
52
53 Define new tag for output
54
55 tag('Title', rec('200','a') );
56
57 By default, output will go into I<search> and I<display>.
58
59 =cut
60
61 my $out;
62
63 sub tag {
64 my $name = shift or die "tag needs name as first argument";
65 return unless (@_);
66 $out->{$name}->{tag} = $name;
67 $out->{$name}->{search} = \@_;
68 $out->{$name}->{display} = \@_;
69 }
70
71 =head2 display
72
73 Define tag just for I<display>
74
75 @v = display('Title', rec('200','a') );
76
77 =cut
78
79 sub display {
80 my $name = shift or die "display needs name as first argument";
81 return unless (@_);
82 $out->{$name}->{tag} = $name;
83 $out->{$name}->{display} = \@_;
84 }
85
86 =head2 search
87
88 Prepare values just for I<search>
89
90 @v = search('Title', rec('200','a') );
91
92 =cut
93
94 sub search {
95 my $name = shift or die "search needs name as first argument";
96 return unless (@_);
97 $out->{$name}->{tag} = $name;
98 $out->{$name}->{search} = \@_;
99 }
100
101 =head2 get_ds
102
103 Return hash formatted as data structure
104
105 my $ds = get_ds();
106
107 =cut
108
109 sub get_ds {
110 return $out;
111 }
112
113 =head2 clean_ds
114
115 Clean data structure hash for next record
116
117 clean_ds();
118
119 =cut
120
121 sub clean_ds {
122 $out = undef;
123 }
124
125 =head2 set_lookup
126
127 Set current lookup hash
128
129 set_lookup( $lookup );
130
131 =cut
132
133 my $lookup;
134
135 sub set_lookup {
136 $lookup = shift or die "no lookup hash";
137 }
138
139 =head2 rec1
140
141 Return all values in some field
142
143 @v = rec1('200')
144
145 TODO: order of values is probably same as in source data, need to investigate that
146
147 =cut
148
149 sub rec1 {
150 my $f = shift;
151 return unless (defined($rec && $rec->{$f}));
152 if (ref($rec->{$f}) eq 'ARRAY') {
153 if (ref($rec->{$f}->[0]) eq 'HASH') {
154 return map { values %{$_} } @{ $rec->{$f} };
155 } else {
156 return @{ $rec->{$f} };
157 }
158 } elsif( defined($rec->{$f}) ) {
159 return $rec->{$f};
160 }
161 }
162
163 =head2 rec2
164
165 Return all values in specific field and subfield
166
167 @v = rec2('200','a')
168
169 =cut
170
171 sub rec2 {
172 my $f = shift;
173 return unless (defined($rec && $rec->{$f}));
174 my $sf = shift;
175 return map { $_->{$sf} } grep { $_->{$sf} } @{ $rec->{$f} };
176 }
177
178 =head2 rec
179
180 syntaxtic sugar for
181
182 @v = rec('200')
183 @v = rec('200','a')
184
185 =cut
186
187 sub rec {
188 if ($#_ == 0) {
189 return rec1(@_);
190 } elsif ($#_ == 1) {
191 return rec2(@_);
192 }
193 }
194
195 =head2 regex
196
197 Apply regex to some or all values
198
199 @v = regex( 's/foo/bar/g', @v );
200
201 =cut
202
203 sub regex {
204 my $r = shift;
205 my @out;
206 #warn "r: $r\n",Dumper(\@_);
207 foreach my $t (@_) {
208 eval "\$t =~ $r";
209 push @out, $t;
210 }
211 return @out;
212 }
213
214 =head2 prefix
215
216 Prefix all values with a string
217
218 @v = prefix( 'my_', @v );
219
220 =cut
221
222 sub prefix {
223 my $p = shift or die "prefix needs string as first argument";
224 return map { $p . $_ } @_;
225 }
226
227 =head2 first
228
229 Return first element
230
231 $v = first( @v );
232
233 =cut
234
235 sub first {
236 my $r = shift;
237 return $r;
238 }
239
240 =head2 lookup
241
242 Consult lookup hashes for some value
243
244 @v = lookup( $v );
245 @v = lookup( @v );
246
247 =cut
248
249 sub lookup {
250 my $k = shift or return;
251 return unless (defined($lookup->{$k}));
252 if (ref($lookup->{$k}) eq 'ARRAY') {
253 return @{ $lookup->{$k} };
254 } else {
255 return $lookup->{$k};
256 }
257 }
258
259 =head2 join_with
260
261 Joins walues with some delimiter
262
263 $v = join_with(", ", @v);
264
265 =cut
266
267 sub join_with {
268 my $d = shift;
269 return join($d, @_);
270 }
271
272 # END
273 1;

  ViewVC Help
Powered by ViewVC 1.1.26