NAME
Attribute::Validate - Validate your subs with attributes
SYNOPSIS
use Attribute::Validate;
use Types::Standard qw/Maybe InstanceOf ArrayRef Str/
use feature 'signatures';
sub install_gentoo: Requires(Maybe[ArrayRef[InstanceOf['Linux::Capable::Computer']]], Str) ($maybe_computers, $hostname) {
# Do something here
}
install_gentoo([$computer1, $computer2], 'Tux');
DESCRIPTION
This module allows you to validate your non-anonymous subs using the powerful attribute syntax of Perl, bringing easy type-checks to your code, thanks to Type::Tiny you can create your own types to enforce your program using the data you expect it to use.
INSTANCE METHODS
This module cannot and shouldn't be instanced.
ATTRIBUTES
Requires
sub say_word: Requires(Str) {
say shift;
}
sub say_word_with_spec: Requires(\%spec, Str) {
say shift;
}
Receives a list of Type::Tiny types and enforces those types into the arguments, the first argument may be a HashRef containing the spec of Type::Params to change the behavior of this module, for example {strictness => 0} as the first argument will allow the user to have more arguments than the ones declared.
EXPORTABLE SUBROUTINES
anon_requires
my $say_thing = anon_requires(sub($thing) {
say $thing;
), Str);
my $say_thing = anon_requires(sub($thing) {
say $thing;
}, \%spec, Str);
Enforces types into anonymous subroutines since those cannot be enchanted using attributes.
DEPENDENCIES
The module will pull all the dependencies it needs on install, the minimum supported Perl is v5.16.3, although latest versions are mostly tested for 5.38.2
CONFIGURATION AND ENVIRONMENT
If your OS Perl is too old perlbrew can be used instead.
BUGS AND LIMITATIONS
Enchanting anonymous subroutines with attributes won't allow them to be used by this module because of limitations of the language.
LICENSE AND COPYRIGHT
This software is Copyright (c) 2025 by Sergio Iglesias.
This is free software, licensed under:
The MIT (X11) License
CREDITS
Thanks to MultiSafePay and the Tech Leader of MultiSafePay for agreeing in creating this CPAN module inspired in a similar feature in their codebase, this code was inspired by code found there, but was written without the code in front from scratch.
MultiSafePay is searching for Perl Developers for working in their offices on Estepona on Spain next to the beach, if you apply and do not get a reply and you think you are a experienced/capable enough Perl Developer drop me a e-mail so I can try to help you get a job mailto:sergioxz@cpan.org.
INCOMPATIBILITIES
None known.
VERSION
0.0.x
AUTHOR
Sergio Iglesias