| 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<List::Filter::Dispatcher> | ||||||||||
| 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<List::Filter::Project/"creating new filter methods">. | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | =head2 METHODS | ||||||||||
| 38 | |||||||||||
| 39 | =cut | ||||||||||
| 40 | |||||||||||
| 41 | 2 | ▶ | use 5.8.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 2 | my $DEBUG = 0; | |||||||||
| 45 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 2 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | |||||||||||
| 48 | 2 | require Exporter; | |||||||||
| 49 | |||||||||||
| 50 | 2 | our @ISA = qw(Exporter); | |||||||||
| 51 | |||||||||||
| 52 | 2 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||
| 53 | ) ] ); | ||||||||||
| 54 | 2 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 55 | |||||||||||
| 56 | 2 | our @EXPORT = qw( | |||||||||
| 57 | find_any | ||||||||||
| 58 | skip_any | ||||||||||
| 59 | match_all_any_order | ||||||||||
| 60 | ); | ||||||||||
| 61 | |||||||||||
| 62 | 2 | 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 | 0 | ▶ | sub find_any { | ||||||||
|
Subroutine
find_any: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 92 | 0 | 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 } ) { | |||||||||
| 102 | 0 | foreach my $pat ( @{ $search_patterns } ) { | |||||||||
| 103 | 0 | my $mpat; | |||||||||
| 104 | 0 | ▶ | if ($modifiers) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | $mpat = "(?$modifiers)$pat"; | |||||||||
| 106 | } else { | ||||||||||
| 107 | 0 | $mpat = $pat; | |||||||||
| 108 | } | ||||||||||
| 109 | 0 | ▶ | if ( $item =~ m{ $mpat }x ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub skip_any { | ||||||||
|
Subroutine
skip_any: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 132 | 0 | 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 } ) { | |||||||||
| 141 | 0 | foreach my $pat ( @{ $search_patterns } ) { | |||||||||
| 142 | 0 | my $mpat; | |||||||||
| 143 | 0 | ▶ | if ($modifiers) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 0 | $mpat = "(?$modifiers)$pat"; | |||||||||
| 145 | } else { | ||||||||||
| 146 | 0 | $mpat = $pat; | |||||||||
| 147 | } | ||||||||||
| 148 | 0 | ▶ | if ( $item =~ m{ $mpat }x ) { # if any pattern matches skip this item | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub match_all_any_order { | ||||||||
|
Subroutine
match_all_any_order: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||
| 179 | 0 | 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 } ); | |||||||||
| 186 | |||||||||||
| 187 | 0 | my @result = (); | |||||||||
| 188 | 0 | foreach my $item (@{ $items }) { | |||||||||
| 189 | 0 | my $pass_count = 0; | |||||||||
| 190 | 0 | my @patterns = @{ $search_patterns }; # copy to avoid munging originals | |||||||||
| 191 | 0 | foreach my $pat (@patterns) { | |||||||||
| 192 | 0 | ▶ | if ( $pat =~ s{^ - (.*?) $}{$1}x ) { # leading hyphens become rev matches | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | |||||||||||
| 194 | 0 | my $mpat; | |||||||||
| 195 | 0 | ▶ | if ($modifiers) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 196 | 0 | $mpat = "(?$modifiers)$pat"; | |||||||||
| 197 | } else { | ||||||||||
| 198 | 0 | $mpat = $pat; | |||||||||
| 199 | } | ||||||||||
| 200 | |||||||||||
| 201 | 0 | ▶ | $pass_count++ if (not( $item =~ m{$mpat}x )); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | } else { | ||||||||||
| 203 | |||||||||||
| 204 | 0 | my $mpat; | |||||||||
| 205 | 0 | ▶ | if ($modifiers) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 206 | 0 | $mpat = "(?$modifiers)$pat"; | |||||||||
| 207 | } else { | ||||||||||
| 208 | 0 | $mpat = $pat; | |||||||||
| 209 | } | ||||||||||
| 210 | |||||||||||
| 211 | 0 | ▶ | $pass_count++ if ( $item =~ m{$mpat}x ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 212 | } | ||||||||||
| 213 | } | ||||||||||
| 214 | 0 | ▶ | if ( $pass_count >= $criteria_count ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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<List::Filter> | ||||||||||
| 229 | |||||||||||
| 230 | =head1 AUTHOR | ||||||||||
| 231 | |||||||||||
| 232 | Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt> | ||||||||||
| 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 | ||||||||||