bump version to 1.19
[gitmo/Moose.git] / lib / Moose / Util / TypeConstraints / OptimizedConstraints.pm
CommitLineData
5d2f0933 1package Moose::Util::TypeConstraints::OptimizedConstraints;
2
3use strict;
4use warnings;
5
e631c7d1 6use Class::MOP;
1fa1a58d 7use Moose::Deprecated;
28412c0b 8use Scalar::Util 'blessed', 'looks_like_number';
9
245478d5 10our $VERSION = '1.19';
75b95414 11$VERSION = eval $VERSION;
28412c0b 12our $AUTHORITY = 'cpan:STEVAN';
5d2f0933 13
fd542f49 14sub Value { defined($_[0]) && !ref($_[0]) }
5d2f0933 15
fd542f49 16sub Ref { ref($_[0]) }
5d2f0933 17
74d5a674 18# We need to use a temporary here to flatten LVALUEs, for instance as in
19# Str(substr($_,0,255)).
20sub Str {
21 my $value = $_[0];
22 defined($value) && ref(\$value) eq 'SCALAR'
23}
bc47531e 24
fd542f49 25sub Num { !ref($_[0]) && looks_like_number($_[0]) }
5d2f0933 26
47bfa72e 27# using a temporary here because regex matching promotes an IV to a PV,
28# and that confuses some things (like JSON.pm)
29sub Int {
30 my $value = $_[0];
31 defined($value) && !ref($value) && $value =~ /^-?[0-9]+$/
32}
fd542f49 33
150e5142 34sub ScalarRef { ref($_[0]) eq 'SCALAR' || ref($_[0]) eq 'REF' }
267f8179 35sub ArrayRef { ref($_[0]) eq 'ARRAY' }
36sub HashRef { ref($_[0]) eq 'HASH' }
37sub CodeRef { ref($_[0]) eq 'CODE' }
38sub RegexpRef { ref($_[0]) eq 'Regexp' }
39sub GlobRef { ref($_[0]) eq 'GLOB' }
fd542f49 40
41sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa("IO::Handle") }
42
43sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
44
0eb7894a 45sub 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}
fd542f49 53
e151db23 54sub ClassName {
e631c7d1 55 return Class::MOP::is_class_loaded( $_[0] );
e151db23 56}
57
f0cac16f 58sub RoleName {
9df65fdb 59 ClassName($_[0])
60 && (Class::MOP::class_of($_[0]) || return)->isa('Moose::Meta::Role')
f0cac16f 61}
62
fd542f49 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
5d2f0933 67
28412c0b 681;
5d2f0933 69
70__END__
71
72=pod
73
74=head1 NAME
75
76Moose::Util::TypeConstraints::OptimizedConstraints - Optimized constraint
77bodies for various moose types
78
5d2f0933 79=head1 DESCRIPTION
80
d03bd989 81This file contains the hand optimized versions of Moose type constraints,
004222dc 82no user serviceable parts inside.
5d2f0933 83
84=head1 FUNCTIONS
85
86=over 4
87
6549b0d1 88=item C<Value>
5d2f0933 89
6549b0d1 90=item C<Ref>
5d2f0933 91
6549b0d1 92=item C<Str>
5d2f0933 93
6549b0d1 94=item C<Num>
5d2f0933 95
6549b0d1 96=item C<Int>
5d2f0933 97
6549b0d1 98=item C<ScalarRef>
5d2f0933 99
6549b0d1 100=item C<ArrayRef>
5d2f0933 101
6549b0d1 102=item C<HashRef>
5d2f0933 103
6549b0d1 104=item C<CodeRef>
5d2f0933 105
6549b0d1 106=item C<RegexpRef>
5d2f0933 107
6549b0d1 108=item C<GlobRef>
5d2f0933 109
6549b0d1 110=item C<FileHandle>
5d2f0933 111
6549b0d1 112=item C<Object>
5d2f0933 113
6549b0d1 114=item C<Role>
5d2f0933 115
6549b0d1 116=item C<ClassName>
e151db23 117
c8a26288 118=item C<RoleName>
119
5d2f0933 120=back
28412c0b 121
122=head1 BUGS
123
d4048ef3 124See L<Moose/BUGS> for details on reporting bugs.
28412c0b 125
126=head1 AUTHOR
127
128Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
129
130=head1 COPYRIGHT AND LICENSE
131
2840a3b2 132Copyright 2007-2009 by Infinity Interactive, Inc.
28412c0b 133
134L<http://www.iinteractive.com>
135
136This library is free software; you can redistribute it and/or modify
137it under the same terms as Perl itself.
138
139=cut