Int type constraint allowed for a newline character due to the /^..$/ syntax. Use...
[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
fd542f49 10sub Value { defined($_[0]) && !ref($_[0]) }
5d2f0933 11
fd542f49 12sub Ref { ref($_[0]) }
5d2f0933 13
720f6a58 14# We might need to use a temporary here to flatten LVALUEs, for instance as in
74d5a674 15# Str(substr($_,0,255)).
16sub Str {
720f6a58 17 defined($_[0])
18 && ( ref(\ $_[0] ) eq 'SCALAR'
19 || ref(\(my $value = $_[0])) eq 'SCALAR')
74d5a674 20}
bc47531e 21
fd542f49 22sub Num { !ref($_[0]) && looks_like_number($_[0]) }
5d2f0933 23
47bfa72e 24# using a temporary here because regex matching promotes an IV to a PV,
25# and that confuses some things (like JSON.pm)
26sub Int {
27 my $value = $_[0];
f55fba5c 28 defined($value) && !ref($value) && $value =~ /\A-?[0-9]+\z/
47bfa72e 29}
fd542f49 30
150e5142 31sub ScalarRef { ref($_[0]) eq 'SCALAR' || ref($_[0]) eq 'REF' }
267f8179 32sub ArrayRef { ref($_[0]) eq 'ARRAY' }
33sub HashRef { ref($_[0]) eq 'HASH' }
34sub CodeRef { ref($_[0]) eq 'CODE' }
35sub RegexpRef { ref($_[0]) eq 'Regexp' }
36sub GlobRef { ref($_[0]) eq 'GLOB' }
fd542f49 37
38sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa("IO::Handle") }
39
40sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
41
0eb7894a 42sub 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?'
47 );
48 blessed( $_[0] ) && $_[0]->can('does');
49}
fd542f49 50
e151db23 51sub ClassName {
e631c7d1 52 return Class::MOP::is_class_loaded( $_[0] );
e151db23 53}
54
f0cac16f 55sub RoleName {
9df65fdb 56 ClassName($_[0])
57 && (Class::MOP::class_of($_[0]) || return)->isa('Moose::Meta::Role')
f0cac16f 58}
59
fd542f49 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
5d2f0933 64
28412c0b 651;
5d2f0933 66
67__END__
68
69=pod
70
71=head1 NAME
72
73Moose::Util::TypeConstraints::OptimizedConstraints - Optimized constraint
74bodies for various moose types
75
5d2f0933 76=head1 DESCRIPTION
77
d03bd989 78This file contains the hand optimized versions of Moose type constraints,
004222dc 79no user serviceable parts inside.
5d2f0933 80
81=head1 FUNCTIONS
82
83=over 4
84
6549b0d1 85=item C<Value>
5d2f0933 86
6549b0d1 87=item C<Ref>
5d2f0933 88
6549b0d1 89=item C<Str>
5d2f0933 90
6549b0d1 91=item C<Num>
5d2f0933 92
6549b0d1 93=item C<Int>
5d2f0933 94
6549b0d1 95=item C<ScalarRef>
5d2f0933 96
6549b0d1 97=item C<ArrayRef>
5d2f0933 98
6549b0d1 99=item C<HashRef>
5d2f0933 100
6549b0d1 101=item C<CodeRef>
5d2f0933 102
6549b0d1 103=item C<RegexpRef>
5d2f0933 104
6549b0d1 105=item C<GlobRef>
5d2f0933 106
6549b0d1 107=item C<FileHandle>
5d2f0933 108
6549b0d1 109=item C<Object>
5d2f0933 110
6549b0d1 111=item C<Role>
5d2f0933 112
6549b0d1 113=item C<ClassName>
e151db23 114
c8a26288 115=item C<RoleName>
116
5d2f0933 117=back
28412c0b 118
119=head1 BUGS
120
d4048ef3 121See L<Moose/BUGS> for details on reporting bugs.
28412c0b 122
28412c0b 123=cut