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