Validate(3pm)
πΈ Validate(3pm) β the Data::Validate era π
okay bestie, gather round, because this is the manual page for Data::Validate (that's the Validate(3pm) one, perl's own little validation baddie) and she is serving common data validation methods. let's get into it π
β¨ SYNOPSIS β¨
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'));
π¦ DESCRIPTION π¦
so basically this module collects common validation routines, and its whole thing is making input validation and untainting easier and more readable. lowkey most of these functions are not even much shorter than their direct perl equivalent (and in some cases they're way longer π) β but their names make it crystal clear what you're trying to test for. it's giving self-documenting code, and honestly? iconic.
almost all functions return an untainted value if the test passes, and undef if it fails. which means β and this is the part people fumble, so listen β you should always check for a defined status explicitly. do not assume the return will be true. (e.g. is_integer(0)) π the value 0 is real and valid and it will absolutely ruin your day if you just do a truthiness check.
the value to test is always the first (and often only) argument. periodt.
π FUNCTIONS π
new β constructor for OO usage
new();
Description returns a Data::Validator object. this lets you access all the validator function calls as methods, without importing them into your namespace, or without using that clumsy Data::Validate::function_name() format. no cap, it's cleaner.
Arguments none
Returns returns a Data::Validate object
is_integer β is the value an integer?
is_integer($value);
Description 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.) so close bestie, but no π«Ά
Arguments
$valueβ the potential integer to test.
Returns returns the untainted integer on success, undef on failure. note that the return can be 0, so always check with defined()
Notes, Exceptions, & Bugs number translation is done by POSIX casting tools (strtol).
is_numeric β is the value numeric?
is_numeric($value);
Description 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) β¨
Arguments
$valueβ the potential number to test.
Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()
Notes, Exceptions, & Bugs number translation is done by POSIX casting tools (strtol).
is_hex β is the value a hex number?
is_hex($value);
Description returns the untainted number if the test value is a hex number.
Arguments
$valueβ the potential number to test.
Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()
Notes, Exceptions, & Bugs none
is_oct β is the value an octal number?
is_oct($value);
Description returns the untainted number if the test value is a octal number.
Arguments
$valueβ the potential number to test.
Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()
Notes, Exceptions, & Bugs none
is_between β is the value between two numbers?
is_between($value, $min, $max);
Description returns the untainted number if the test value is numeric, and falls between $min and $max inclusive. and here's the cute part: 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 range is unlimited, bestie π
Arguments
$valueβ the potential number to test.$minβ the minimum valid value. unlimited if set toundef$maxβ the maximum valid value. unlimited if set toundef
Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()
is_greater_than β is the value greater than a threshold?
is_greater_than($value, $threshold);
Description returns the untainted number if the test value is numeric, and is greater than $threshold. (not inclusive)
Arguments
$valueβ the potential number to test.$thresholdβ the minimum value (non-inclusive)
Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()
is_less_than β is the value less than a threshold?
is_less_than($value, $threshold);
Description returns the untainted number if the test value is numeric, and is less than $threshold. (not inclusive)
Arguments
$valueβ the potential number to test.$thresholdβ the maximum value (non-inclusive)
Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()
is_equal_to β do a string/number neutral ==
is_equal_to($value, $target);
Description 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) π« yeah. undefined does not equal undefined here. it's giving chaos but it's giving documented chaos.
Arguments
$valueβ the value to test.$targetβ the value to test against
Returns 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);
Description returns the untainted $value if it's numeric, an integer, and even.
Arguments
$valueβ the value to test.
Returns returns $value (untainted). note that the return can be 0, so always check with defined().
is_odd β is a number odd?
is_odd($value);
Description returns the untainted $value if it's numeric, an integer, and odd.
Arguments
$valueβ the value to test.
Returns returns $value (untainted). note that the return can be 0, so always check with defined().
is_alphanumeric β does it only contain letters and numbers?
is_alphanumeric($value);
Description returns the untainted $value if it is defined and only contains letters (upper or lower case) and numbers. also allows an empty string β ''. yes, empty string is valid, she's allowed to be nothing π
Arguments
$valueβ the value to test.
Returns returns $value (untainted). note that the return can be 0, so always check with defined().
is_printable β does it only contain printable characters?
is_alphanumeric($value);
Description 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 β ''.
Arguments
$valueβ the value to test.
Returns returns $value (untainted). note that the return can be 0, so always check with defined().
length_is_between β is the string length between two limits?
length_is_between($value, $min, $max);
Description 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.
Arguments
$valueβ the value to test.$minβ the minimum length of the string (inclusive).$maxβ the maximum length of the string (inclusive).
Returns returns $value. note that the return can be 0, so always check with defined(). the value is not automatically untainted.
π AUTHOR π
Richard Sonnen <sonnen@richardsonnen.com>.
π COPYRIGHT π
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.
π POD ERRORS π
hey! the above document had some coding errors, which are explained below:
around line 89:
You can't have =items (as at line 97) unless the first thing after
the =over is an =item
not the doc dragging itself in its own pod errors π even the manual page isn't immune to a little syntax slip, and honestly? relatable. slay anyway.
and that's the whole thing, bestie β perl v5.38.2, dated 2024-03-05, page Validate(3pm), signing off π«Άβ¨ validate ur inputs, check for defined(), and stay untainted π
π¦ where this page comes from
- debian trixie: libdata-validate-perl 0.09-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdata-validate-perl 0.09-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdata-validate-perl 0.09-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdata-validate-perl 0.09-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms