/[cvs]/nfo/perl/libs/Regexp/Group.pm
ViewVC logotype

Annotation of /nfo/perl/libs/Regexp/Group.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Thu Mar 27 15:45:00 2003 UTC (21 years, 1 month ago) by joko
Branch: MAIN
Changes since 1.3: +5 -2 lines
fixes to modules regarding new namespace(s) below Data::Mungle::*

1 joko 1.1 ## ------------------------------------------------------------------------
2 joko 1.4 ## $Id: Group.pm,v 1.3 2003/02/20 21:11:43 joko Exp $
3 joko 1.1 ## ------------------------------------------------------------------------
4 joko 1.2 ## $Log: Group.pm,v $
5 joko 1.4 ## Revision 1.3 2003/02/20 21:11:43 joko
6     ## renamed module
7     ##
8 joko 1.3 ## Revision 1.2 2003/02/14 14:17:42 joko
9     ## + more warnings and verbosing
10     ##
11 joko 1.2 ## Revision 1.1 2002/12/22 14:16:39 joko
12     ## + initial check-in
13     ##
14 joko 1.1 ## ------------------------------------------------------------------------
15    
16    
17     package Regexp::Group;
18    
19     use strict;
20     use warnings;
21    
22 joko 1.3 use base qw( DesignPattern::Logger );
23 joko 1.1
24    
25     use Data::Dumper;
26    
27 joko 1.4 use Data::Mungle::Compare::Struct qw( isEmpty );
28 joko 1.1
29     my @patterns;
30    
31     # ------------ common perl object constructor ------------
32     sub new {
33     my $invocant = shift;
34     my $class = ref($invocant) || $invocant;
35     my @args = ();
36     @_ && (@args = @_);
37     #$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"!
38     my $self = { @_ };
39     bless $self, $class;
40     $self->{caller} = caller;
41    
42     #print Dumper(caller(2));
43     #exit;
44    
45     $self->_init();
46     return $self;
47     }
48    
49     sub _init {
50     my $self = shift;
51    
52     # TODO/REVIEW: compile patterns?
53     #@patterns = map{ qr/$_/ } @patterns;
54    
55     # dereference main/raw data (payload)
56     $self->{data} = ${$self->{data}};
57     }
58    
59     sub scan {
60     my $self = shift;
61     my $stepkeys = shift;
62     my $coderef = shift;
63    
64     #my $nodecount_default = shift;
65     my $nodecount_default = 19;
66    
67     #print "SCAN", "\n";
68     #print $self->{pattern}, "\n";
69     $self->{result} = [];
70     $self->{result_raw} = [];
71    
72     # configure tracing
73     # respects additional trace-options passed to _trace-method
74     $self->{TRACE_OPTIONS} = 1;
75     # disables _any_ tracing - even if sub-conditions evaluate to true values
76     $self->{TRACE_DISABLED} = 0;
77    
78     $self->log('starting to scan');
79    
80     #for (1..20) {
81    
82     my $patterncount = 0;
83     my $match_in_loop;
84    
85     my $steps;
86     if ($stepkeys) {
87     $steps = $stepkeys;
88     $patterncount++;
89     } else {
90     $steps = $self->{metadata}->{steps};
91     }
92    
93     #print "cb-2: $coderef", "\n";
94    
95     foreach my $stepkey_current (@{$steps}) {
96     $self->_scan_step($stepkey_current, $coderef);
97     $patterncount++;
98     }
99    
100     #}
101    
102     # ... propagate them to processing
103    
104     # assign result to property of object's current instance
105     #push @{$self->{result}}, $result;
106    
107     #$self->{code}->($self->{caller}, $self);
108    
109     }
110    
111     sub _scan_step {
112    
113     my $self = shift;
114     my $stepkey = shift;
115     my $callback = shift;
116    
117     #print "cb-3: $callback", "\n";
118    
119     #$self->log("running step '$stepkey'", 'debug');
120     $self->log("running step '$stepkey'");
121    
122     my $dataref = $self->{data};
123     my $metadata = $self->{metadata}->{$stepkey};
124     my $pattern = $self->{patterns}->{$stepkey};
125     my $coderef = $self->{coderefs}->{$stepkey};
126    
127     #print Dumper($coderef);
128     #print Dumper($self->{data});
129     #exit;
130    
131     $self->trace("data used", $dataref, 0, undef, { tag => '', exit => 0 });
132     $self->trace("metadata used", $metadata, 0, undef, { tag => '', exit => 0 });
133     $self->trace("pattern used", $pattern, 0, undef, { tag => '', exit => 0 });
134     $self->trace("coderef used", $coderef, 0, undef, { tag => '', exit => 0 });
135    
136     #print "pattern: $pattern", "\n";
137     #exit;
138    
139     # if metadata variable "noscan" is supplied, don't kick off the regex-engine, just call the coderef
140     # TODO: log this event
141     if ($metadata->{noscan} && $metadata->{noscan} == 1) {
142     $coderef->($self, undef, undef, $callback);
143     return;
144     }
145    
146     # dereference data if its still referenced
147     my $data = ${$self->{data}};
148    
149 joko 1.2 if (!$data) {
150     $self->log("data is empty: stepkey=$stepkey", 'warning');
151     return;
152     }
153    
154 joko 1.1 #$self->{data} =~ s{
155     $data =~ s{
156    
157     $pattern # the pattern itself - as contained in a string - actually gets interpolated here
158    
159     }{
160    
161     print "." if $self->{verbose};
162    
163     $self->log("match in step '$stepkey'");
164    
165     # have a clean item to fill in slots
166     my $result_item = [];
167    
168     # 1. build matched structure from slots - grouping functionality for slots
169    
170     # V1 - hand-rolled
171     #my $result = [$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20];
172     #print Dumper($result);
173    
174     my $count = $metadata->{nodecount};
175    
176     # TODO: re-renable this!
177     #$count ||= $nodecount_default;
178    
179     # V2 - iterate!
180     # push dummy item to slot '0' to mimic known behaviour of accessing $1, $2, $3, ....
181     push @{$result_item}, 'n/a';
182     for my $i (1..$count) {
183     my $slot = '$' . $i;
184     push @{$result_item}, eval($slot);
185     }
186    
187    
188     # 2. remember built result (raw) in object - grouping functionality for items
189     $self->log("pushing raw result");
190     push @{$self->{result_raw}}, $result_item;
191    
192    
193     # 4. check/modify further metadata
194     # e.g. 'column_positions' means: "this step is a ColumnProvider"
195     #print Dumper($metadata);
196     if (my $positions = $metadata->{column_positions}) {
197     $self->trace('positions', $positions, 0);
198    
199     #print Dumper($result_item);
200    
201     # build column-names array from position-metadata
202     my @column_names;
203     map {
204     push @column_names, $result_item->[$_];
205     } @$positions;
206    
207     # store additional metadata
208     $metadata->{column_names} = \@column_names;
209     }
210    
211     $self->trace('column-names', $metadata->{column_names}, 0);
212    
213    
214     #return '';
215     #$&;
216     push(@{$self->{matches}->{parsed_chunks}}, $&);
217     #undef;
218    
219     my $matchpos = pos($data);
220     $self->{matches}->{position} = $matchpos;
221     push @{$self->{matches}->{positions}}, $matchpos;
222    
223    
224     # 3. call coderef to get _processed_ result if given (callback)
225     $self->log("calling coderef '$coderef'");
226     if ($coderef) {
227     my $result_processed = {};
228     if ($coderef->($self, $result_item, $result_processed, $callback)) {
229     $self->trace("pushing processed result", $result_processed, 0);
230     push @{$self->{result}}, $result_processed if $result_processed;
231     } else {
232     $self->log('processing of container failed', 'warning');
233     return;
234     }
235     } else {
236     # TODO: croak 'no coderef';
237     }
238    
239     # pass back matched content if we are not the ones to delete something (pass-back-if-not-remover)
240     # with this mechanism, we can iterate over the same content again and again and don't loose data while doing this
241     # without this, multiple scan stepping would not be possible since each step would either remove its
242     # matched content - or not: no flexibility.
243     # as it seems we need flexibility at any point ;-( in order not to break our necks with code-maintenance ;-)
244     # this also can get configured here (this flag is contained in the metadata-part of your filter-declaration)
245     $& if !$metadata->{remover};
246    
247     }xge;
248    
249    
250     #$self->{data_rest} = \$data;
251     $self->{data} = \$data;
252    
253     #$self->trace('matches', $self->getMatches(), 1);
254     #$self->trace('matches-positions', $self->getMatchPositions(), 1);
255 joko 1.2
256     #print "\r" if $self->{verbose};
257     print " " x 50 if $self->{verbose};
258     print "\r" if $self->{verbose};
259 joko 1.1
260     }
261    
262     sub continue {
263     my $self = shift;
264     my $scankey = shift;
265     #print "CONTINUE!", "\n";
266     #$self->{data} = $self->{data_rest};
267     return $self->scan($scankey);
268     }
269    
270     sub getMatches {
271     my $self = shift;
272     return $self->{result_raw};
273     }
274    
275     sub getResults {
276     my $self = shift;
277     return $self->{result};
278     }
279    
280     sub getDataRest {
281     my $self = shift;
282     return $self->{data_rest};
283     }
284    
285     sub getMatchCount {
286     my $self = shift;
287    
288     my $matches = $self->getMatches();
289     return if isEmpty($matches);
290    
291     my $count = $#{$matches};
292     $count++;
293     return $count;
294     }
295    
296     sub getMatchPositions {
297     my $self = shift;
298     if (!$self->getMatchCount()) {
299     $self->{matches}->{positions} = [];
300     }
301     return $self->{matches}->{positions};
302     }
303    
304     sub getMatchPosition {
305     my $self = shift;
306     return $self->{matches}->{position};
307     }
308    
309     1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed