Explicitly use Moose::Deprecated in all modules that call ::deprecated()
[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 Class::MOP;
7 use Moose::Deprecated;
8 use Scalar::Util 'blessed', 'looks_like_number';
9
10 our $VERSION   = '1.08';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
13
14 sub Value { defined($_[0]) && !ref($_[0]) }
15
16 sub Ref { ref($_[0]) }
17
18 # We need to use a temporary here to flatten LVALUEs, for instance as in
19 # Str(substr($_,0,255)).
20 sub Str {
21     my $value = $_[0];
22     defined($value) && ref(\$value) eq 'SCALAR'
23 }
24
25 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
26
27 sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
28
29 sub ScalarRef { ref($_[0]) eq 'SCALAR' || ref($_[0]) eq 'REF' }
30 sub ArrayRef  { ref($_[0]) eq 'ARRAY'  }
31 sub HashRef   { ref($_[0]) eq 'HASH'   }
32 sub CodeRef   { ref($_[0]) eq 'CODE'   }
33 sub RegexpRef { ref($_[0]) eq 'Regexp' }
34 sub GlobRef   { ref($_[0]) eq 'GLOB'   }
35
36 sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa("IO::Handle") }
37
38 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
39
40 sub Role {
41     Moose::Deprecated::deprecated(
42         feature => 'Role type',
43         message =>
44             'The Role type has been deprecated. Maybe you meant to create a RoleName type?'
45     );
46     blessed( $_[0] ) && $_[0]->can('does');
47 }
48
49 sub ClassName {
50     return Class::MOP::is_class_loaded( $_[0] );
51 }
52
53 sub RoleName {
54     ClassName($_[0])
55     && (Class::MOP::class_of($_[0]) || return)->isa('Moose::Meta::Role')
56 }
57
58 # NOTE:
59 # we have XS versions too, ...
60 # 04:09 <@konobi> nothingmuch: konobi.co.uk/code/utilsxs.tar.gz
61 # 04:09 <@konobi> or utilxs.tar.gz iirc
62
63 1;
64
65 __END__
66
67 =pod
68
69 =head1 NAME
70
71 Moose::Util::TypeConstraints::OptimizedConstraints - Optimized constraint
72 bodies for various moose types
73
74 =head1 DESCRIPTION
75
76 This file contains the hand optimized versions of Moose type constraints,
77 no user serviceable parts inside.
78
79 =head1 FUNCTIONS
80
81 =over 4
82
83 =item C<Value>
84
85 =item C<Ref>
86
87 =item C<Str>
88
89 =item C<Num>
90
91 =item C<Int>
92
93 =item C<ScalarRef>
94
95 =item C<ArrayRef>
96
97 =item C<HashRef>
98
99 =item C<CodeRef>
100
101 =item C<RegexpRef>
102
103 =item C<GlobRef>
104
105 =item C<FileHandle>
106
107 =item C<Object>
108
109 =item C<Role>
110
111 =item C<ClassName>
112
113 =item C<RoleName>
114
115 =back
116
117 =head1 BUGS
118
119 See L<Moose/BUGS> for details on reporting bugs.
120
121 =head1 AUTHOR
122
123 Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
124
125 =head1 COPYRIGHT AND LICENSE
126
127 Copyright 2007-2009 by Infinity Interactive, Inc.
128
129 L<http://www.iinteractive.com>
130
131 This library is free software; you can redistribute it and/or modify
132 it under the same terms as Perl itself.
133
134 =cut