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