bump version to 0.70
[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.70';
9 $VERSION = eval $VERSION;
10 our $AUTHORITY = 'cpan:STEVAN';
11
12 sub Value { defined($_[0]) && !ref($_[0]) }
13
14 sub Ref { ref($_[0]) }
15
16 sub Str { defined($_[0]) && !ref($_[0]) }
17
18 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
19
20 sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
21
22 sub ScalarRef { ref($_[0]) eq 'SCALAR' }
23 sub ArrayRef  { ref($_[0]) eq 'ARRAY'  }
24 sub HashRef   { ref($_[0]) eq 'HASH'   }
25 sub CodeRef   { ref($_[0]) eq 'CODE'   }
26 sub RegexpRef { ref($_[0]) eq 'Regexp' }
27 sub GlobRef   { ref($_[0]) eq 'GLOB'   }
28
29 sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa("IO::Handle") }
30
31 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
32
33 sub Role { blessed($_[0]) && $_[0]->can('does') }
34
35 sub ClassName {
36     return 0 if ref($_[0]) || !defined($_[0]) || !length($_[0]);
37
38     # walk the symbol table tree to avoid autovififying
39     # \*{${main::}{"Foo::"}} == \*main::Foo::
40
41     my $pack = \*::;
42     foreach my $part (split('::', $_[0])) {
43         return 0 unless exists ${$$pack}{"${part}::"};
44         $pack = \*{${$$pack}{"${part}::"}};
45     }
46
47     # check for $VERSION or @ISA
48     return 1 if exists ${$$pack}{VERSION}
49              && defined *{${$$pack}{VERSION}}{SCALAR};
50     return 1 if exists ${$$pack}{ISA}
51              && defined *{${$$pack}{ISA}}{ARRAY};
52
53     # check for any method
54     foreach ( keys %{$$pack} ) {
55         next if substr($_, -2, 2) eq '::';
56         return 1 if defined *{${$$pack}{$_}}{CODE};
57     }
58
59     # fail
60     return 0;
61 }
62
63 sub RoleName {
64     ClassName($_[0]) 
65         && (($_[0]->can('meta') || return)->($_[0]) || return)->isa('Moose::Meta::Role')
66 }
67
68 # NOTE:
69 # we have XS versions too, ...
70 # 04:09 <@konobi> nothingmuch: konobi.co.uk/code/utilsxs.tar.gz
71 # 04:09 <@konobi> or utilxs.tar.gz iirc
72
73 1;
74
75 __END__
76
77 =pod
78
79 =head1 NAME
80
81 Moose::Util::TypeConstraints::OptimizedConstraints - Optimized constraint
82 bodies for various moose types
83
84 =head1 DESCRIPTION
85
86 This file contains the hand optimized versions of Moose type constraints, 
87 no user serviceable parts inside.
88
89 =head1 FUNCTIONS
90
91 =over 4
92
93 =item C<Value>
94
95 =item C<Ref>
96
97 =item C<Str>
98
99 =item C<Num>
100
101 =item C<Int>
102
103 =item C<ScalarRef>
104
105 =item C<ArrayRef>
106
107 =item C<HashRef>
108
109 =item C<CodeRef>
110
111 =item C<RegexpRef>
112
113 =item C<GlobRef>
114
115 =item C<FileHandle>
116
117 =item C<Object>
118
119 =item C<Role>
120
121 =item C<ClassName>
122
123 =item C<RoleName>
124
125 =back
126
127 =head1 BUGS
128
129 All complex software has bugs lurking in it, and this module is no 
130 exception. If you find a bug please either email me, or add the bug
131 to cpan-RT.
132
133 =head1 AUTHOR
134
135 Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
136
137 =head1 COPYRIGHT AND LICENSE
138
139 Copyright 2007-2009 by Infinity Interactive, Inc.
140
141 L<http://www.iinteractive.com>
142
143 This library is free software; you can redistribute it and/or modify
144 it under the same terms as Perl itself.
145
146 =cut