Mouse::Util::blessed
[gitmo/Mouse.git] / lib / Mouse / TypeRegistry.pm
CommitLineData
d60c78b9 1#!/usr/bin/env perl
2package Mouse::TypeRegistry;
3use strict;
4use warnings;
9baf5d6b 5
8fcbe7fb 6use Mouse::Util 'blessed';
7use Scalar::Util qw/looks_like_number openhandle/;
d60c78b9 8
0f636a97 9no warnings 'uninitialized';
d60c78b9 10sub optimized_constraints {
11 return {
12 Any => sub { 1 },
13 Item => sub { 1 },
14 Bool => sub {
15 !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
16 },
f5fbe3cc 17 Undef => sub { !defined($_) },
18 Defined => sub { defined($_) },
0f636a97 19 Value => sub { defined($_) && !ref($_) },
20 Num => sub { !ref($_) && looks_like_number($_) },
21 Int => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
22 Str => sub { defined($_) && !ref($_) },
79af4b55 23 ClassName => sub { Mouse::is_class_loaded($_) },
0f636a97 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' },
d60c78b9 42 };
43}
44
451;
46
6feb83f1 47__END__
48
49=head1 NAME
50
51Mouse::TypeRegistry - simple type constraints
52
53=head1 METHODS
54
55=head2 optimized_constraints -> HashRef[CODE]
56
57Returns the simple type constraints that Mouse understands.
58
59=cut
60
61