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
49 This module exports everthing that Params::Validate does except for the
50 "validate" and "validate_pos" subroutines.
51
52 ATTRIBUTES
53
54 * Validate
55 This attribute corresponse to the "validate" subroutine in
56 Params::Validate.
57
58 * ValidatePos
59 This attribute corresponse to the "validate_pos" subroutine in
60 Params::Validate.
61
62 OO
63
64 If you are using this module to mark methods for validation, as opposed
65 to subroutines, it is crucial that you mark these methods with the
66 ":method" attribute, as well as the "Validate" or "ValidatePos"
67 attribute.
68
69 If you do not do this, then the object or class used in the method call
70 will be passed to the validation routines, which is probably not what
71 you want.
72
73 CAVEATS
74
75 You must put all the arguments to the "Validate" or "ValidatePos"
76 attribute on a single line, or Perl will complain.
77
79 Params::Validate
80
82 Dave Rolsky, <autarch@urth.org>
83
84
85
86perl v5.8.8 2006-01-21 Attribute::Params::Validate(3)