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