Commit | Line | Data |
---|---|---|
5d2f0933 | 1 | package Moose::Util::TypeConstraints::OptimizedConstraints; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
28412c0b | 6 | use Scalar::Util 'blessed', 'looks_like_number'; |
7 | ||
d344c3fe | 8 | our $VERSION = '0.68'; |
75b95414 | 9 | $VERSION = eval $VERSION; |
28412c0b | 10 | our $AUTHORITY = 'cpan:STEVAN'; |
5d2f0933 | 11 | |
fd542f49 | 12 | sub Value { defined($_[0]) && !ref($_[0]) } |
5d2f0933 | 13 | |
fd542f49 | 14 | sub Ref { ref($_[0]) } |
5d2f0933 | 15 | |
fd542f49 | 16 | sub Str { defined($_[0]) && !ref($_[0]) } |
bc47531e | 17 | |
fd542f49 | 18 | sub Num { !ref($_[0]) && looks_like_number($_[0]) } |
5d2f0933 | 19 | |
fd542f49 | 20 | sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ } |
21 | ||
267f8179 | 22 | sub ScalarRef { ref($_[0]) eq 'SCALAR' } |
23 | sub ArrayRef { ref($_[0]) eq 'ARRAY' } | |
24 | sub HashRef { ref($_[0]) eq 'HASH' } | |
25 | sub CodeRef { ref($_[0]) eq 'CODE' } | |
26 | sub RegexpRef { ref($_[0]) eq 'Regexp' } | |
27 | sub GlobRef { ref($_[0]) eq 'GLOB' } | |
fd542f49 | 28 | |
29 | sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa("IO::Handle") } | |
30 | ||
31 | sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' } | |
32 | ||
33 | sub Role { blessed($_[0]) && $_[0]->can('does') } | |
34 | ||
e151db23 | 35 | sub ClassName { |
36 | return 0 if ref($_[0]) || !defined($_[0]) || !length($_[0]); | |
37 | ||
38 | # walk the symbol table tree to avoid autovififying | |
39 | # \*{${main::}{"Foo::"}} == \*main::Foo:: | |
40 | ||
41 | my $pack = \*::; | |
42 | foreach my $part (split('::', $_[0])) { | |
43 | return 0 unless exists ${$$pack}{"${part}::"}; | |
44 | $pack = \*{${$$pack}{"${part}::"}}; | |
45 | } | |
46 | ||
47 | # check for $VERSION or @ISA | |
48 | return 1 if exists ${$$pack}{VERSION} | |
49 | && defined *{${$$pack}{VERSION}}{SCALAR}; | |
50 | return 1 if exists ${$$pack}{ISA} | |
51 | && defined *{${$$pack}{ISA}}{ARRAY}; | |
52 | ||
53 | # check for any method | |
54 | foreach ( keys %{$$pack} ) { | |
55 | next if substr($_, -2, 2) eq '::'; | |
56 | return 1 if defined *{${$$pack}{$_}}{CODE}; | |
57 | } | |
58 | ||
59 | # fail | |
60 | return 0; | |
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 | 68 | 1; |
5d2f0933 | 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 | ||
5d2f0933 | 79 | =head1 DESCRIPTION |
80 | ||
004222dc | 81 | This file contains the hand optimized versions of Moose type constraints, |
82 | no 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 | |
5d2f0933 | 118 | =back |
28412c0b | 119 | |
120 | =head1 BUGS | |
121 | ||
122 | All complex software has bugs lurking in it, and this module is no | |
123 | exception. If you find a bug please either email me, or add the bug | |
124 | to cpan-RT. | |
125 | ||
126 | =head1 AUTHOR | |
127 | ||
128 | Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt> | |
129 | ||
130 | =head1 COPYRIGHT AND LICENSE | |
131 | ||
2840a3b2 | 132 | Copyright 2007-2009 by Infinity Interactive, Inc. |
28412c0b | 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 |