Data::Validate - common data validation methods
use Data::Validate qw(:math); if(defined(is_integer($suspect))){ print "Looks like an integer\n"; } my $name = is_alphanumeric($suspect); if(defined($name)){ print "$name is alphanumeric, and has been untainted\n"; } else { print "$suspect was not alphanumeric" } # or as an object my $v = Data::Validate->new(); die "'foo' is not an integer" unless defined($v->is_integer('foo'));
This module collects common validation routines to make input validation, and untainting easier and more readable. Most of the functions are not much shorter than their direct perl equivalent (and are much longer in some cases), but their names make it clear what you're trying to test for.
Almost all functions return an untainted value if the test passes, and undef if it fails. This means that you should always check for a defined status explicitly. Don't assume the return will be true. (e.g. is_integer(0))
The value to test is always the first (and often only) argument.
new - constructor for OO usage
new();
Returns a Data::Validator object. This lets you access all the validator function calls as methods without importing them into your namespace or using the clumsy Data::Validate::function_name() format.
None
Returns a Data::Validate object
is_integer - is the value an integer?
is_integer($value);
Returns the untainted number if the test value is an integer, or can be cast to one without a loss of precision. (i.e. 1.0 is considered an integer, but 1.0001 is not.)
The potential integer to test.
Returns the untainted integer on success, undef on failure. Note that the return can be 0, so always check with defined()
Number translation is done by POSIX casting tools (strtol).
is_numeric - is the value numeric?
is_numeric($value);
Returns the untainted number if the test value is numeric according to Perl's own internal rules. (actually a wrapper on Scalar::Util::looks_like_number)
The potential number to test.
Returns the untainted number on success, undef on failure. Note that the return can be 0, so always check with defined()
is_hex - is the value a hex number?
is_hex($value);
Returns the untainted number if the test value is a hex number.
is_oct - is the value an octal number?
is_oct($value);
Returns the untainted number if the test value is a octal number.
is_between - is the value between two numbers?
is_between($value, $min, $max);
Returns the untainted number if the test value is numeric, and falls between $min and $max inclusive. Note that either $min or $max can be undef, which means 'unlimited'. i.e. is_between($val, 0, undef) would pass for any number zero or larger.
The minimum valid value. Unlimited if set to undef
The maximum valid value. Unlimited if set to undef
is_greater_than - is the value greater than a threshold?
is_greater_than($value, $threshold);
Returns the untainted number if the test value is numeric, and is greater than $threshold. (not inclusive)
The minimum value (non-inclusive)
is_less_than - is the value less than a threshold?
is_less_than($value, $threshold);
Returns the untainted number if the test value is numeric, and is less than $threshold. (not inclusive)
The maximum value (non-inclusive)
is_equal_to - do a string/number neutral ==
is_equal_to($value, $target);
Returns the target if $value is equal to it. Does a math comparison if both $value and $target are numeric, or a string comparison otherwise. Both the $value and $target must be defined to get a true return. (i.e. undef != undef)
The value to test.
The value to test against
Unlike most validator routines, this one does not necessarily untaint its return value, it just returns $target. This has the effect of untainting if the target is a constant or other clean value. (i.e. is_equal_to($bar, 'foo')). Note that the return can be 0, so always check with defined()
is_even - is a number even?
is_even($value);
Returns the untainted $value if it's numeric, an integer, and even.
Returns $value (untainted). Note that the return can be 0, so always check with defined().
is_odd - is a number odd?
is_odd($value);
Returns the untainted $value if it's numeric, an integer, and odd.
is_alphanumeric - does it only contain letters and numbers?
is_alphanumeric($value);
Returns the untainted $value if it is defined and only contains letters (upper or lower case) and numbers. Also allows an empty string - ''.
is_printable - does it only contain printable characters?
Returns the untainted $value if it is defined and only contains printable characters as defined by the composite POSIX character class [[:print:][:space:]]. Also allows an empty string - ''.
length_is_between - is the string length between two limits?
length_is_between($value, $min, $max);
Returns $value if it is defined and its length is between $min and $max inclusive. Note that this function does not untaint the value.
If either $min or $max are undefined they are treated as no-limit.
The minimum length of the string (inclusive).
The maximum length of the string (inclusive).
Returns $value. Note that the return can be 0, so always check with defined(). The value is not automatically untainted.
Richard Sonnen <sonnen@richardsonnen.com>.
Copyright (c) 2004 Richard Sonnen. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
You can't have =items (as at line 96) unless the first thing after the =over is an =item
To install Data::Validate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Validate
CPAN shell
perl -MCPAN -e shell install Data::Validate
For more information on module installation, please visit the detailed CPAN module installation guide.