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