/usr/share/perl5/Data/FormValidator/Filters.pm is in libdata-formvalidator-perl 4.81-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | # Filters.pm - Common filters for use in Data::FormValidator.
# This file is part of Data::FormValidator.
#
# Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
# Maintainer: Mark Stosberg <mark@summersault.com>
#
# Copyright (C) 1999,2000 iNsu Innovations Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms same terms as perl itself.
package Data::FormValidator::Filters;
use Exporter 'import';
use strict;
our $VERSION = 4.81;
our @EXPORT_OK = qw(
filter_alphanum
filter_decimal
filter_digit
filter_dollars
filter_integer
filter_lc
filter_neg_decimal
filter_neg_integer
filter_phone
filter_pos_decimal
filter_pos_integer
filter_quotemeta
filter_sql_wildcard
filter_strip
filter_trim
filter_uc
filter_ucfirst
FV_split
FV_replace
);
our %EXPORT_TAGS = (
filters => \@EXPORT_OK,
);
sub DESTROY {}
=pod
=head1 NAME
Data::FormValidator::Filters - Basic set of filters available in an Data::FormValidator profile.
=head1 SYNOPSIS
use Data::FormValidator;
%profile = (
filters => 'trim',
...
);
my $results = Data::FormValidator->check( \%data, \%profile );
=head1 DESCRIPTION
These are the builtin filters which may be specified as a name in the
I<filters>, I<field_filters>, and I<field_filter_regexp_map> parameters of the
input profile.
Filters are applied as the first step of validation, possibly modifying a copy
of the validation before any constraints are checked.
=head1 RECOMMENDED USE
As a long time maintainer and user of Data::FormValidator, I recommend that
filters be used with caution. They are immediately modifying the input
provided, so the original data is lost. The few I recommend include C<trim>,
which removes leading and trailing whitespace. I have this turned on by default
by using L<CGI::Application::Plugin::ValidateRM>. It's also generally safe to use
the C<lc> and C<uc> filters if you need that kind of data transformation.
Beyond simple filters, I recommend transforming the C<"valid"> hash returned
from validation if further changes are needed.
=head1 PROCEDURAL INTERFACE
You may also call these functions directly through the
procedural interface by either importing them directly or importing the whole
I<:filters> group. For example, if you want to access the I<trim> function
directly, you could either do:
use Data::FormValidator::Filters (qw/filter_trim/);
# or
use Data::FormValidator::Filters (qw/:filters/);
$string = filter_trim($string);
Notice that when you call filters directly, you'll need to prefix the filter name with
"filter_".
=head1 THE FILTERS
=head2 FV_split
use Data::FormValidator::Filters qw(FV_split);
# Validate every e-mail in a comma separated list
field_filters => {
several_emails => FV_split(qr/\s*,\s*/),
# Any pattern that can be used by the 'split' builtin works.
tab_sep_field => FV_split('\t'),
},
constraint_methods => {
several_emails => email(),
},
With this filter, you can split a field into multiple values. The constraint for
the field will then be applied to every value.
This filter has a different naming convention because it is a higher-order
function. Rather than returning a value directly, it returns a code reference
to a standard Data::FormValidator filter.
After successfully being validated the values will appear as an arrayref.
=cut
sub FV_split {
my $splitter = shift || die "nothing to split on!";
return sub {
my $value = shift;
return undef unless defined $value;
my @a = split $splitter, $value;
return \@a;
};
}
=head2 FV_replace
use Data::FormValidator::Filters qw(FV_replace);
field_filters => {
first_name => FV_replace(qr/Mark/,'Don'),
},
FV_replace is a shorthand for writing simple find-and-replace filters.
The above filter would be translated to this:
sub { my $v = shift; $v =~ s/Mark/Don/; $v }
For more complex filters, just write your own.
=cut
sub FV_replace {
my ($find,$replace) = @_;
return sub {
my $v = shift;
$v =~ s/$find/$replace/;
return $v;
};
}
=head2 trim
Remove white space at the front and end of the fields.
=cut
sub filter_trim {
my $value = shift;
return unless defined $value;
# Remove whitespace at the front
$value =~ s/^\s+//o;
# Remove whitespace at the end
$value =~ s/\s+$//o;
return $value;
}
=pod
=head2 strip
Runs of white space are replaced by a single space.
=cut
sub filter_strip {
my $value = shift;
return unless defined $value;
# Strip whitespace
$value =~ s/\s+/ /g;
return $value;
}
=pod
=head2 digit
Remove non digits characters from the input.
=cut
sub filter_digit {
my $value = shift;
return unless defined $value;
$value =~ s/\D//g;
return $value;
}
=pod
=head2 alphanum
Remove non alphanumeric characters from the input.
=cut
sub filter_alphanum {
my $value = shift;
return unless defined $value;
$value =~ s/\W//g;
return $value;
}
=pod
=head2 integer
Extract from its input a valid integer number.
=cut
sub filter_integer {
my $value = shift;
return unless defined $value;
$value =~ tr/0-9+-//dc;
($value) =~ m/([-+]?\d+)/;
return $value;
}
=pod
=head2 pos_integer
Extract from its input a valid positive integer number.
Bugs: This filter won't extract "9" from "a9+", it will instead extract "9+"
=cut
sub filter_pos_integer {
my $value = shift;
return unless defined $value;
$value =~ tr/0-9+//dc;
($value) =~ m/(\+?\d+)/;
return $value;
}
=pod
=head2 neg_integer
Extract from its input a valid negative integer number.
Bugs: This filter will currently filter the case of "a9-" to become "9-",
which it should leave it alone.
=cut
sub filter_neg_integer {
my $value = shift;
return unless defined $value;
$value =~ tr/0-9-//dc;
($value) =~ m/(-\d+)/;
return $value;
}
=pod
=head2 decimal
Extract from its input a valid decimal number.
Bugs: Given "1,000.23", it will currently return "1.000.23"
=cut
sub filter_decimal {
my $value = shift;
return unless defined $value;
# This is a localization problem, but anyhow...
$value =~ tr/,/./;
$value =~ tr/0-9.+-//dc;
($value) =~ m/([-+]?\d+\.?\d*)/;
return $value;
}
=pod
=head2 pos_decimal
Extract from its input a valid positive decimal number.
Bugs: Given "1,000.23", it will currently return "1.000.23"
=cut
sub filter_pos_decimal {
my $value = shift;
return unless defined $value;
# This is a localization problem, but anyhow...
$value =~ tr/,/./;
$value =~ tr/0-9.+//dc;
($value) =~ m/(\+?\d+\.?\d*)/;
return $value;
}
=pod
=head2 neg_decimal
Extract from its input a valid negative decimal number.
Bugs: Given "1,000.23", it will currently return "1.000.23"
=cut
sub filter_neg_decimal {
my $value = shift;
return unless defined $value;
# This is a localization problem, but anyhow...
$value =~ tr/,/./;
$value =~ tr/0-9.-//dc;
($value) =~ m/(-\d+\.?\d*)/;
return $value;
}
=pod
=head2 dollars
Extract from its input a valid number to express dollars like currency.
Bugs: This filter won't currently remove trailing numbers like "1.234".
=cut
sub filter_dollars {
my $value = shift;
return unless defined $value;
$value =~ tr/,/./;
$value =~ tr/0-9.+-//dc;
($value) =~ m/(\d+\.?\d?\d?)/;
return $value;
}
=pod
=head2 phone
Filters out characters which aren't valid for an phone number. (Only
accept digits [0-9], space, comma, minus, parenthesis, period and pound [#].)
=cut
sub filter_phone {
my $value = shift;
return unless defined $value;
$value =~ s/[^\d,\(\)\.\s,\-#]//g;
return $value;
}
=pod
=head2 sql_wildcard
Transforms shell glob wildcard (*) to the SQL like wildcard (%).
=cut
sub filter_sql_wildcard {
my $value = shift;
return unless defined $value;
$value =~ tr/*/%/;
return $value;
}
=pod
=head2 quotemeta
Calls the quotemeta (quote non alphanumeric character) builtin on its
input.
=cut
sub filter_quotemeta {
return unless defined $_[0];
quotemeta $_[0];
}
=pod
=head2 lc
Calls the lc (convert to lowercase) builtin on its input.
=cut
sub filter_lc {
return unless defined $_[0];
lc $_[0];
}
=pod
=head2 uc
Calls the uc (convert to uppercase) builtin on its input.
=cut
sub filter_uc {
return unless defined $_[0];
uc $_[0];
}
=pod
=head2 ucfirst
Calls the ucfirst (Uppercase first letter) builtin on its input.
=cut
sub filter_ucfirst {
return unless defined $_[0];
ucfirst $_[0];
}
1;
__END__
=head1 SEE ALSO
=over 4
=item o
L<Data::FormValidator>
=item o
L<Data::FormValidator::Constraints>
=item o
L<Data::FormValidator::Filters::Image> - shrink incoming image uploads
=back
=head1 AUTHOR
Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
Maintainer: Mark Stosberg <mark@summersault.com>
=head1 COPYRIGHT
Copyright (c) 1999,2000 iNsu Innovations Inc.
All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms as perl itself.
=cut
|