3ad1f888d3e2cda78b3cff56002dbe1d8b1a941d
[gitmo/Moose.git] / lib / Moose / Util / TypeConstraints / OptimizedConstraints.pm
1 package Moose::Util::TypeConstraints::OptimizedConstraints;
2
3 use strict;
4 use warnings;
5
6 use Scalar::Util 'blessed', 'looks_like_number';
7
8 our $VERSION   = '0.02';
9 our $AUTHORITY = 'cpan:STEVAN';
10
11 sub Value { defined($_[0]) && !ref($_[0]) }
12
13 sub Ref { ref($_[0]) }
14
15 sub Str { defined($_[0]) && !ref($_[0]) }
16
17 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
18
19 sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
20
21 {
22     no warnings 'uninitialized';
23     sub ScalarRef { ref($_[0]) eq 'SCALAR' }
24     sub ArrayRef  { ref($_[0]) eq 'ARRAY'  }
25     sub HashRef   { ref($_[0]) eq 'HASH'   }
26     sub CodeRef   { ref($_[0]) eq 'CODE'   }
27     sub RegexpRef { ref($_[0]) eq 'Regexp' }
28     sub GlobRef   { ref($_[0]) eq 'GLOB'   }
29 }
30
31 sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa("IO::Handle") }
32
33 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
34
35 sub Role { blessed($_[0]) && $_[0]->can('does') }
36
37 # NOTE:
38 # we have XS versions too, ...
39 # 04:09 <@konobi> nothingmuch: konobi.co.uk/code/utilsxs.tar.gz
40 # 04:09 <@konobi> or utilxs.tar.gz iirc
41
42 1;
43
44 __END__
45
46 =pod
47
48 =head1 NAME
49
50 Moose::Util::TypeConstraints::OptimizedConstraints - Optimized constraint
51 bodies for various moose types
52
53 =head1 DESCRIPTION
54
55 This file contains the hand optimized versions of Moose type constraints, 
56 no user serviceable parts inside.
57
58 =head1 FUNCTIONS
59
60 =over 4
61
62 =item Value
63
64 =item Ref
65
66 =item Str
67
68 =item Num
69
70 =item Int
71
72 =item ScalarRef
73
74 =item ArrayRef
75
76 =item HashRef
77
78 =item CodeRef
79
80 =item RegexpRef
81
82 =item GlobRef
83
84 =item FileHandle
85
86 =item Object
87
88 =item Role
89
90 =back
91
92 =head1 BUGS
93
94 All complex software has bugs lurking in it, and this module is no 
95 exception. If you find a bug please either email me, or add the bug
96 to cpan-RT.
97
98 =head1 AUTHOR
99
100 Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
101
102 =head1 COPYRIGHT AND LICENSE
103
104 Copyright 2007-2008 by Infinity Interactive, Inc.
105
106 L<http://www.iinteractive.com>
107
108 This library is free software; you can redistribute it and/or modify
109 it under the same terms as Perl itself.
110
111 =cut