Commit | Line | Data |
d60c78b9 |
1 | #!/usr/bin/env perl |
2 | package Mouse::TypeRegistry; |
3 | use strict; |
4 | use warnings; |
0f636a97 |
5 | use Scalar::Util qw/looks_like_number blessed openhandle/; |
d60c78b9 |
6 | |
0f636a97 |
7 | no warnings 'uninitialized'; |
d60c78b9 |
8 | sub optimized_constraints { |
9 | return { |
10 | Any => sub { 1 }, |
11 | Item => sub { 1 }, |
12 | Bool => sub { |
13 | !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' |
14 | }, |
f5fbe3cc |
15 | Undef => sub { !defined($_) }, |
16 | Defined => sub { defined($_) }, |
0f636a97 |
17 | Value => sub { defined($_) && !ref($_) }, |
18 | Num => sub { !ref($_) && looks_like_number($_) }, |
19 | Int => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ }, |
20 | Str => sub { defined($_) && !ref($_) }, |
79af4b55 |
21 | ClassName => sub { Mouse::is_class_loaded($_) }, |
0f636a97 |
22 | Ref => sub { ref($_) }, |
23 | |
24 | ScalarRef => sub { ref($_) eq 'SCALAR' }, |
25 | ArrayRef => sub { ref($_) eq 'ARRAY' }, |
26 | HashRef => sub { ref($_) eq 'HASH' }, |
27 | CodeRef => sub { ref($_) eq 'CODE' }, |
28 | RegexpRef => sub { ref($_) eq 'Regexp' }, |
29 | GlobRef => sub { ref($_) eq 'GLOB' }, |
30 | |
31 | FileHandle => sub { |
32 | ref($_) eq 'GLOB' |
33 | && openhandle($_) |
34 | or |
35 | blessed($_) |
36 | && $_->isa("IO::Handle") |
37 | }, |
38 | |
39 | Object => sub { blessed($_) && blessed($_) ne 'Regexp' }, |
d60c78b9 |
40 | }; |
41 | } |
42 | |
43 | 1; |
44 | |
6feb83f1 |
45 | __END__ |
46 | |
47 | =head1 NAME |
48 | |
49 | Mouse::TypeRegistry - simple type constraints |
50 | |
51 | =head1 METHODS |
52 | |
53 | =head2 optimized_constraints -> HashRef[CODE] |
54 | |
55 | Returns the simple type constraints that Mouse understands. |
56 | |
57 | =cut |
58 | |
59 | |