Attribute::Params::Validate - Validate method/function parameters using attributes
use Attribute::Params::Validate qw(:all);
# takes named params (hash or hashref) # foo is mandatory, bar is optional sub foo : Validate( foo => 1, bar => 0 ) { ... }
# takes positional params # first two are mandatory, third is optional sub bar : ValidatePos( 1, 1, 0 ) { ... }
# for some reason Perl insists that the entire attribute be on one line sub foo2 : Validate( foo => { type => ARRAYREF }, bar => { can => [ 'print', 'flush', 'frobnicate' ] }, baz => { type => SCALAR, callbacks => { 'numbers only' => sub { shift() =~ /^\d+$/ }, 'less than 90' => sub { shift() < 90 } } } ) { ... }
# note that this is marked as a method. This is very important! sub baz : Validate( foo => { type => ARRAYREF }, bar => { isa => 'Frobnicator' } ) method { ... }
Please see Params::Validate for more information on how you can specify what validation is performed.
If you do not do this, then the object or class used in the method call will be passed to the validation routines, which is probably not what you want.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |