have ClassName check use is_class_loaded
[gitmo/Mouse.git] / lib / Mouse / TypeRegistry.pm
1 #!/usr/bin/env perl
2 package Mouse::TypeRegistry;
3 use strict;
4 use warnings;
5 use Scalar::Util qw/looks_like_number blessed openhandle/;
6
7 no warnings 'uninitialized';
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         },
15         Undef      => sub { !defined($_) },
16         Defined    => sub { defined($_) },
17         Value      => sub { defined($_) && !ref($_) },
18         Num        => sub { !ref($_) && looks_like_number($_) },
19         Int        => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
20         Str        => sub { defined($_) && !ref($_) },
21         ClassName  => sub { Mouse::is_class_loaded($_) },
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' },
40     };
41 }
42
43 1;
44
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