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