Data::Type::Guard - inspects members of foreign objects
my $dtg = Data::Type::Guard->new ( allow => [ 'Human', 'Others' ], # blessed objects of that type tests => { email => STD::EMAIL( 1 ), # mxcheck ON ! see Email::Valid firstname => STD::WORD, social_id => [ STD::NUM, STD::VARCHAR( 10 ) ], contacts => sub { my %args = @_; exists $args{lucy} }, } ); die "object is rejected" unless $dtg->inspect( $h ); # compact version valid_object { email => STD::EMAIL( 1 ), firstname => STD::WORD }, $object_a, $object_b;
This class inspects others objects member return-values for a specific datatype.
my $dtg = Data::Type::Guard->new( allow => $aref, tests => $href )
If is set then the inspect function below will return 0 if the object is not a reference of the requested type. If empty, isn't selective for special references ( HASH, ARRAY, "CUSTOM", .. ).
inspect
0
Keys are the members names (anything that can be called via the $o->member syntax) and the type(s) as value. When a member should match multple types, they should be contained in an array reference ( i.e. 'fon' => [ qw(NUM TELEPHONE) ] ). Instead of types a reference to a sub is allowed, while it must return true if it matches (see valid_object() TYPE, .. }, @objects )">).
Accepts a blessed reference as a parameter. It returns 0 if a guard test or type constrain will fail, otherwise 1.
1
[Note] A more appropriate report on failure is planned.
Valids members of objects against multiple 'types' or code reference. Any $object must have an accessor function to its method (same as the key given in the member $href). See Data::Type::Guard for oo-interface for that.
$object
member
$href
my $car = Car->new( speed => 300, year => '2000', owner_firstname => 'micheal' ); valid_object( { year => DATE( 'YEAR' ), owner_firstname => VARCHAR(20), speed => INT }, $car ) or die;
$Data::Type::Guard::AUTO_DETECT controls if information from the reflection of an object is superseeding the tests => parameter. Visit Class::Maker reflection manual ( for the types = {}> field ). Example:
types =
use Data::Type qw(:all); use Data::Type::Guard; use Class::Maker qw(class); class 'My::Car', { public => { string => [qw( name manufactorer )], int => [qw( serial )], }, types => { name => STD::WORD, serial => STD::NUM, }, }; my $dtg = Data::Type::Guard->new(); die "My::Car isnt correctly initialized" unless $dtg->inspect( My::Car->new( serial => 'aaaaaa', name => '111' ) );
[Note] This feature is available after Class::Maker '0.5.18', but this is still an undocumented yet.
None per default.
valid_object.
valid_object
':all' loads qw(valid_object)
Sourceforge http://sf.net/projects/datatype is hosting a project dedicated to this module. And I enjoy receiving your comments/suggestion/reports also via http://rt.cpan.org or http://testers.cpan.org.
Murat Uenalan, <muenalan@cpan.org>
To install Data::Type, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Type
CPAN shell
perl -MCPAN -e shell install Data::Type
For more information on module installation, please visit the detailed CPAN module installation guide.