NAME
Simo::Constrain - Constrain methods for Simo;
VERSION
Version 0.01_01
SYNOPSIS
This class provede many methods intended to use with constrain option.
package Book;
use Simo;
use Simo::Constrain qw( is_str isa is_int );
sub title{ ac constrain => sub{ is_str } };
sub author{ ac constrain => sub{ isa 'Person' } }
sub price{ ac constrain => sub{ is_int } }
EXPORT
No function is exported.
All function can be exported.
use Simo::Constrain qw( isa is_object is_int is_str );
FUNCTIONS
The following is each subroutin sourse code.
If function return false, error message is set to $@.
is_undef
my $val = shift || $_;
!defined($val) or $@ = "must be undef.( $val is bad )", return 0;
return 1;
is_defined
my $val = shift || $_;
defined($val) or $@ = "must be defined.( undef is bad )", return 0;
return 1;
is_bool
my $val = shift || $_;
!defined($val) || $val eq "" || "$val" eq '1' || "$val" eq '0'
or $@ = "must be boolean.( $val is bad )", return 0;
return 1;
is_value
my $val = shift || $_;
is_defined( $val ) or return 0;
!ref($val) or $@ = "must be value.( $val is bad )", return 0;
return 1;
is_str
my $val = shift || $_;
is_defined( $val ) or return 0;
!ref($val) or $@ = "must be string.( $val is bad )", return 0;
return 1;
is_num
my $val = shift || $_;
is_defined( $val ) or return 0;
require Scalar::Util;
is_value( $val ) && Scalar::Util::looks_like_number( $val )
or $@ = "must be number.( $val is bad )", return 0;
return 1;
is_int
my $val = shift || $_;
is_defined( $val ) or return 0;
is_num( $val ) && "$val" =~ /^-?[0-9]+$/
or $@ = "must be integer.( $val is bad )", return 0;
return 1;
is_ref
my $val = shift || $_;
is_defined( $val ) or return 0;
ref($val) or $@ = "must be reference.( $val is bad )", return 0;
return 1;
is_scalar_ref
my $val = shift || $_;
is_defined( $val ) or return 0;
is_ref( $val ) && ref($val) eq 'SCALAR'
or $@ = "must be scalar reference.( $val is bad )", return 0;
return 1;
is_array_ref
my $val = shift || $_;
is_defined( $val ) or return 0;
is_ref( $val ) && ref($val) eq 'ARRAY'
or $@ = "must be array reference.( $val is bad )", return 0;
return 1;
is_hash_ref
my $val = shift || $_;
is_defined( $val ) or return 0;
is_ref( $val ) && ref($val) eq 'HASH'
or $@ = "must be hash reference.( $val is bad )", return 0;
return 1;
is_code_ref
my $val = shift || $_;
is_defined( $val ) or return 0;
is_ref( $val ) && ref($val) eq 'CODE'
or $@ = "must be code reference.( $val is bad )", return 0;
return 1;
is_regexp_ref
my $val = shift || $_;
is_defined( $val ) or return 0;
is_ref( $val ) && ref($val) eq 'Regexp'
or $@ = "must be regexp reference.( $val is bad )", return 0;
return 1;
is_glob_ref
my $val = shift || $_;
is_defined( $val ) or return 0;
is_ref( $val ) && ref($val) eq 'GLOB'
or $@ = "must be glob reference.( $val is bad )", return 0;
return 1;
is_file_handle
my $val = shift || $_;
is_defined( $val ) or return 0;
is_glob_ref( $val ) && Scalar::Util::openhandle($val)
or $@ = "must be file handle.( $val is bad )", return 0;
return 1;
is_object
my $val = shift || $_;
is_defined( $val ) or return 0;
require Scalar::Util;
is_ref( $val ) && Scalar::Util::blessed($val) && Scalar::Util::blessed($val) ne 'Regexp'
or $@ = "must be object.( $val is bad )", return 0;
return 1;
is_class_name
my $val = shift || $_;
is_defined( $val ) or return 0;
is_str( $val ) && $val =~ /^(\w+::)*\w+$/ or $@ = "must be class name.( $val is bad )", return 0;
return 1;
is_method_name
my $val = shift || $_;
is_defined( $val ) or return 0;
is_str( $val ) && $val =~ /^[a-zA-Z]\w*$/ or $@ = "must be method name.( $val is bad )", return 0;
return 1;
blessed
my $val = shift || $_;
is_defined( $val ) or return 0;
require Scalar::Util;
Scalar::Util::blessed( $val ) or $@ = "must be blessed.( $val is bad )", return 0;
return 1;
isa
my $val = @_ eq 2 ? shift : $_;
my $class = shift;
croak "class name of isa must be defined" unless defined $class;
croak "class name of isa is invalid" unless is_class_name( $class );
is_defined( $val ) or return 0;
eval{ $val->isa( $class ) } or $@ = "must inherit $class.( $val is bad )", return 0;
return 1;
AUTHOR
Yuki, <kimoto.yuki at gmail.com>
BUGS
Please report any bugs or feature requests to bug-simo-constrain at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Simo-Constrain. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Simo::Constrain
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
See also
I study from Moose::Util::TypeConstraints and Most of Simo::Constrain functions is compatible of Moose::Util::TypeConstraints
Moose,Moose::Util::TypeConstraints
COPYRIGHT & LICENSE
Copyright 2009 Yuki, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.