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