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