Prep for removing deprecated features or making them throw an error.
[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 sub Value { defined($_[0]) && !ref($_[0]) }
11
12 sub Ref { ref($_[0]) }
13
14 # We might need to use a temporary here to flatten LVALUEs, for instance as in
15 # Str(substr($_,0,255)).
16 sub Str {
17     defined($_[0])
18       && (   ref(\             $_[0] ) eq 'SCALAR'
19           || ref(\(my $value = $_[0])) eq 'SCALAR')
20 }
21
22 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
23
24 # using a temporary here because regex matching promotes an IV to a PV,
25 # and that confuses some things (like JSON.pm)
26 sub Int {
27     my $value = $_[0];
28     defined($value) && !ref($value) && $value =~ /\A-?[0-9]+\z/
29 }
30
31 sub ScalarRef { ref($_[0]) eq 'SCALAR' || ref($_[0]) eq 'REF' }
32 sub ArrayRef  { ref($_[0]) eq 'ARRAY'  }
33 sub HashRef   { ref($_[0]) eq 'HASH'   }
34 sub CodeRef   { ref($_[0]) eq 'CODE'   }
35 sub RegexpRef { ref($_[0]) eq 'Regexp' }
36 sub GlobRef   { ref($_[0]) eq 'GLOB'   }
37
38 sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa("IO::Handle") }
39
40 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
41
42 sub Role {
43     Moose::Deprecated::deprecated(
44         feature => 'Role type',
45         message =>
46             'The Role type has been deprecated. Maybe you meant to create a RoleName type? This type be will be removed in Moose 2.0200.'
47     );
48     blessed( $_[0] ) && $_[0]->can('does');
49 }
50
51 sub ClassName {
52     return Class::MOP::is_class_loaded( $_[0] );
53 }
54
55 sub RoleName {
56     ClassName($_[0])
57     && (Class::MOP::class_of($_[0]) || return)->isa('Moose::Meta::Role')
58 }
59
60 # NOTE:
61 # we have XS versions too, ...
62 # 04:09 <@konobi> nothingmuch: konobi.co.uk/code/utilsxs.tar.gz
63 # 04:09 <@konobi> or utilxs.tar.gz iirc
64
65 1;
66
67 __END__
68
69 =pod
70
71 =head1 NAME
72
73 Moose::Util::TypeConstraints::OptimizedConstraints - Optimized constraint
74 bodies for various moose types
75
76 =head1 DESCRIPTION
77
78 This file contains the hand optimized versions of Moose type constraints,
79 no user serviceable parts inside.
80
81 =head1 FUNCTIONS
82
83 =over 4
84
85 =item C<Value>
86
87 =item C<Ref>
88
89 =item C<Str>
90
91 =item C<Num>
92
93 =item C<Int>
94
95 =item C<ScalarRef>
96
97 =item C<ArrayRef>
98
99 =item C<HashRef>
100
101 =item C<CodeRef>
102
103 =item C<RegexpRef>
104
105 =item C<GlobRef>
106
107 =item C<FileHandle>
108
109 =item C<Object>
110
111 =item C<Role>
112
113 =item C<ClassName>
114
115 =item C<RoleName>
116
117 =back
118
119 =head1 BUGS
120
121 See L<Moose/BUGS> for details on reporting bugs.
122
123 =cut