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