Type::Simple - simple type validation system for Perl
Version 0.01
use Type::Simple qw(:all); # simple values validate( Int(), 123 ); # -> true validate( Str(), 'xyz' ); # -> false # compound values validate( ArrayRef(Int()), [ 1, 2, 3 ] ); # -> true validate( HashRef(Bool()), { foo => 1, bar => 0 } ); # -> true
You can pass your own validation functions as code references:
my $greater_than_one = sub { $_[0] > 1 }; my $less_than_ten = sub { $_[0] < 10 }; validate( $greater_than_one, 50 ); # -> true validate( $less_than_ten, 50 ); # -> false
It's possible to combine and modify tests using the boolean functions Type::Simple::AND(), Type::Simple::OR() and Type::Simple::NOT():
Type::Simple::AND()
Type::Simple::OR()
Type::Simple::NOT()
validate( Type::Simple::OR( CodeRef(), RegexpRef() ), $code_or_regexp, ); validate( Type::Simple::AND( Num(), $greater_than_one, $less_than_ten ), $number ); validate( Type::Simple::AND( Num(), Type::Simple::NOT(Int()), ), $non_integer_number );
Any Bool Maybe Undef Defined Value Str Alpha Alnum Ascii Num Int Print Punct Space Word Ref ScalarRef ArrayRef HashRef CodeRef RegexpRef Object
None by default.
All the subroutines below can be exported:
Try to validate a value using a type. Example:
validate( Num(), 123 ); # -> true validate( Num(), 'x' ); # -> false
The validation functions are the following:
Anything.
Perl boolean values: 1, 0, '' and undef.
1
0
''
undef
Type `a or undef.
undef.
Defined value. (Not undef)
Number or string. (Not references)
String.
Since numbers can be stringified, it will also accept numbers. If you want a non-numeric string, you can use:
Type::Simple::AND( Str(), Type::Simple::NOT(Num()), );
A string of alphabetical characters ([A-Za-z]).
[A-Za-z]
A string of alphanumeric characters ([A-Za-z0-9])
[A-Za-z0-9]
A string of characters in the ASCII character set.
Looks like a number.
An integer.
A string of printable characters, including spaces.
A string of non-alphanumeric, non-space characters (<[-!"#$%&'()*+,./:;<=?@[\\\]^_`{|}~]>>).
<[-!"#$%&'()*+,./:;<=
A string of whitespace characters (equivalent to \s).
\s
A string of word characters ([A-Za-z0-9_], equivalent to \w).
[A-Za-z0-9_]
\w
A reference.
A scalar reference.
An array reference.
If you specify `a, the array elements should be of type `a.
A hash reference.
If you specify `a, the hash values should be of type `a.
A code reference.
A regexp reference.
A blessed object.
Nelson Ferraz, <nferraz at gmail.com>
<nferraz at gmail.com>
Please report any bugs or feature requests to https://github.com/nferraz/type-simple/issues. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Type::Simple
You can also look for information at:
GitHub
http://github.com/nferraz/type-simple
Search CPAN
http://search.cpan.org/dist/Type-Simple/
Copyright 2017 Nelson Ferraz.
This program is distributed under the MIT (X11) License: http://www.opensource.org/licenses/mit-license.php
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
To install Type::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Type::Simple
CPAN shell
perl -MCPAN -e shell install Type::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.