line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package List::Filter::Filters::Standard; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
List::Filter::Filters::Standard - define standard List::Filter filter methods |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# This module is not intended to be used directly |
10
|
|
|
|
|
|
|
# See: L |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 DESCRIPTION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
This module defines the standard List::Filter filter methods. |
15
|
|
|
|
|
|
|
This is to say that it supplies a series of methods that can be |
16
|
|
|
|
|
|
|
used with a List::Filter "filter" to do the actual filtering |
17
|
|
|
|
|
|
|
operations upon a list of input strings. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Note that this module uses Exporter to export all of it's subs, |
20
|
|
|
|
|
|
|
but these subs must be written as methods (e.g. with "my $self = |
21
|
|
|
|
|
|
|
shift" as first the line), because that's how they'll ultimately |
22
|
|
|
|
|
|
|
be used. Here "$self" is a "Dispatcher" object, and has nothing |
23
|
|
|
|
|
|
|
to do with this package. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head2 interface |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Each of these subs begins with: |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $self = shift; |
30
|
|
|
|
|
|
|
my $filter = shift; # object (href based) |
31
|
|
|
|
|
|
|
my $items = shift; # aref |
32
|
|
|
|
|
|
|
my $opt = shift; # href of options (which is itself optional) |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
And each returns an aref. See L. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head2 METHODS |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
2
|
|
6371
|
use 5.8.0; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
107
|
|
42
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
86
|
|
43
|
2
|
|
|
2
|
|
114
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
87
|
|
44
|
|
|
|
|
|
|
my $DEBUG = 0; |
45
|
2
|
|
|
2
|
|
11
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
161
|
|
46
|
2
|
|
|
2
|
|
13
|
use Data::Dumper; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1744
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
require Exporter; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
53
|
|
|
|
|
|
|
) ] ); |
54
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
our @EXPORT = qw( |
57
|
|
|
|
|
|
|
find_any |
58
|
|
|
|
|
|
|
skip_any |
59
|
|
|
|
|
|
|
match_all_any_order |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 search filter filter methods |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
These are the methods that apply a list of search criteria. Each |
67
|
|
|
|
|
|
|
search filter specifies one of these methods to use by default. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
They all have identical interfaces: they take two input |
70
|
|
|
|
|
|
|
arguments, a search filter and a reference to a list of strings |
71
|
|
|
|
|
|
|
to be filtered, and they return an array reference of strings |
72
|
|
|
|
|
|
|
that meet the search criteria. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=over |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=item find_any |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Pass through an item if it matches any one of the search terms/patterns. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Input: |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
(1) a search filter structure (which contains an aref of search patterns), |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
(2) arrayref of items to be searched |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Return: arrayref of items that match all criteria. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub find_any { |
92
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
93
|
0
|
|
|
|
|
|
my $filter = shift; |
94
|
0
|
|
|
|
|
|
my $items = shift; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my $search_patterns = $filter->{ terms }; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
my $modifiers = $filter->{modifiers}; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
my @result = (); |
101
|
0
|
|
|
|
|
|
ITEM: foreach my $item ( @{ $items } ) { |
|
0
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
foreach my $pat ( @{ $search_patterns } ) { |
|
0
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
my $mpat; |
104
|
0
|
0
|
|
|
|
|
if ($modifiers) { |
105
|
0
|
|
|
|
|
|
$mpat = "(?$modifiers)$pat"; |
106
|
|
|
|
|
|
|
} else { |
107
|
0
|
|
|
|
|
|
$mpat = $pat; |
108
|
|
|
|
|
|
|
} |
109
|
0
|
0
|
|
|
|
|
if ( $item =~ m{ $mpat }x ) { |
110
|
0
|
|
|
|
|
|
push @result, $item; |
111
|
0
|
|
|
|
|
|
next ITEM; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
0
|
|
|
|
|
|
return \@result; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item skip_any |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Pass through an item only if it does not match any of the search |
122
|
|
|
|
|
|
|
terms/patterns. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Input: |
125
|
|
|
|
|
|
|
(1) a search filter structure (which contains an aref of search patterns), |
126
|
|
|
|
|
|
|
(2) arrayref of items to be searched |
127
|
|
|
|
|
|
|
Return: arrayref of items that match all criteria. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub skip_any { |
132
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
133
|
0
|
|
|
|
|
|
my $filter = shift; |
134
|
0
|
|
|
|
|
|
my $items = shift; |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $search_patterns = $filter->{ terms }; |
137
|
0
|
|
|
|
|
|
my $modifiers = $filter->{ modifiers }; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
my @result = (); |
140
|
0
|
|
|
|
|
|
ITEM: foreach my $item ( @{ $items } ) { |
|
0
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
foreach my $pat ( @{ $search_patterns } ) { |
|
0
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
my $mpat; |
143
|
0
|
0
|
|
|
|
|
if ($modifiers) { |
144
|
0
|
|
|
|
|
|
$mpat = "(?$modifiers)$pat"; |
145
|
|
|
|
|
|
|
} else { |
146
|
0
|
|
|
|
|
|
$mpat = $pat; |
147
|
|
|
|
|
|
|
} |
148
|
0
|
0
|
|
|
|
|
if ( $item =~ m{ $mpat }x ) { # if any pattern matches skip this item |
149
|
0
|
|
|
|
|
|
next ITEM; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
# made it through the gauntlet, so keep this item |
153
|
0
|
|
|
|
|
|
push @result, $item; |
154
|
|
|
|
|
|
|
} |
155
|
0
|
|
|
|
|
|
return \@result; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item match_all_any_order |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Pass through an item if it matches all of the search criteria |
162
|
|
|
|
|
|
|
(irrespective of the the order of the matches inside the string). |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
On each term of the search criteria, a leading minus sign may |
165
|
|
|
|
|
|
|
reverse the sense of the match. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Input: |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
(1) a search filter structure (which contains an aref of |
170
|
|
|
|
|
|
|
search patterns), |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
(2) arrayref of items to be searched |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Return: arrayref of items that match all criteria. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub match_all_any_order { |
179
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
180
|
0
|
|
|
|
|
|
my $filter = shift; |
181
|
0
|
|
|
|
|
|
my $items = shift; |
182
|
0
|
|
|
|
|
|
my $search_patterns = $filter->{ terms }; |
183
|
0
|
|
|
|
|
|
my $modifiers = $filter->{modifiers}; |
184
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
|
my $criteria_count = scalar( @{ $search_patterns } ); |
|
0
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
0
|
|
|
|
|
|
my @result = (); |
188
|
0
|
|
|
|
|
|
foreach my $item (@{ $items }) { |
|
0
|
|
|
|
|
|
|
189
|
0
|
|
|
|
|
|
my $pass_count = 0; |
190
|
0
|
|
|
|
|
|
my @patterns = @{ $search_patterns }; # copy to avoid munging originals |
|
0
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
foreach my $pat (@patterns) { |
192
|
0
|
0
|
|
|
|
|
if ( $pat =~ s{^ - (.*?) $}{$1}x ) { # leading hyphens become rev matches |
193
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
my $mpat; |
195
|
0
|
0
|
|
|
|
|
if ($modifiers) { |
196
|
0
|
|
|
|
|
|
$mpat = "(?$modifiers)$pat"; |
197
|
|
|
|
|
|
|
} else { |
198
|
0
|
|
|
|
|
|
$mpat = $pat; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
0
|
0
|
|
|
|
|
$pass_count++ if (not( $item =~ m{$mpat}x )); |
202
|
|
|
|
|
|
|
} else { |
203
|
|
|
|
|
|
|
|
204
|
0
|
|
|
|
|
|
my $mpat; |
205
|
0
|
0
|
|
|
|
|
if ($modifiers) { |
206
|
0
|
|
|
|
|
|
$mpat = "(?$modifiers)$pat"; |
207
|
|
|
|
|
|
|
} else { |
208
|
0
|
|
|
|
|
|
$mpat = $pat; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
0
|
0
|
|
|
|
|
$pass_count++ if ( $item =~ m{$mpat}x ); |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
0
|
0
|
|
|
|
|
if ( $pass_count >= $criteria_count ) { |
215
|
0
|
|
|
|
|
|
push @result, $item; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
} |
218
|
0
|
|
|
|
|
|
return \@result; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=back |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
1; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head1 SEE ALSO |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
L |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 AUTHOR |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Joseph Brenner, Edoom@kzsu.stanford.eduE |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Copyright (C) 2007 by Joseph Brenner |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
239
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.2 or, |
240
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head1 BUGS |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
None reported... yet. |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=cut |