1Attribute::Params::ValiUdsaetre(C3o)ntributed Perl DocumAetnttraitbiuotne::Params::Validate(3)
2
3
4
6 Attribute::Params::Validate - Validate method/function parameters using
7 attributes
8
10 use Attribute::Params::Validate qw(:all);
11
12 # takes named params (hash or hashref)
13 # foo is mandatory, bar is optional
14 sub foo : Validate( foo => 1, bar => 0 )
15 {
16 ...
17 }
18
19 # takes positional params
20 # first two are mandatory, third is optional
21 sub bar : ValidatePos( 1, 1, 0 )
22 {
23 ...
24 }
25
26 # for some reason Perl insists that the entire attribute be on one line
27 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 } } } )
28 {
29 ...
30 }
31
32 # note that this is marked as a method. This is very important!
33 sub baz : Validate( foo => { type => ARRAYREF }, bar => { isa => 'Frobnicator' } ) method
34 {
35 ...
36 }
37
39 The Attribute::Params::Validate module allows you to validate method or
40 function call parameters just like Params::Validate does. However,
41 this module allows you to specify your validation spec as an attribute,
42 rather than by calling the "validate" routine.
43
44 Please see Params::Validate for more information on how you can specify
45 what validation is performed.
46
47 EXPORT
48 This module exports everthing that Params::Validate does except for the
49 "validate" and "validate_pos" subroutines.
50
51 ATTRIBUTES
52 · Validate
53
54 This attribute corresponse to the "validate" subroutine in
55 Params::Validate.
56
57 · ValidatePos
58
59 This attribute corresponse to the "validate_pos" subroutine in
60 Params::Validate.
61
62 OO
63 If you are using this module to mark methods for validation, as opposed
64 to subroutines, it is crucial that you mark these methods with the
65 ":method" attribute, as well as the "Validate" or "ValidatePos"
66 attribute.
67
68 If you do not do this, then the object or class used in the method call
69 will be passed to the validation routines, which is probably not what
70 you want.
71
72 CAVEATS
73 You must put all the arguments to the "Validate" or "ValidatePos"
74 attribute on a single line, or Perl will complain.
75
77 Params::Validate
78
80 Dave Rolsky, <autarch@urth.org>
81
82
83
84perl v5.10.1 2010-11-12 Attribute::Params::Validate(3)