2 package Mouse::TypeRegistry;
5 use Scalar::Util qw/looks_like_number blessed openhandle/;
7 no warnings 'uninitialized';
8 sub optimized_constraints {
13 !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
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 Ref => sub { ref($_) },
23 ScalarRef => sub { ref($_) eq 'SCALAR' },
24 ArrayRef => sub { ref($_) eq 'ARRAY' },
25 HashRef => sub { ref($_) eq 'HASH' },
26 CodeRef => sub { ref($_) eq 'CODE' },
27 RegexpRef => sub { ref($_) eq 'Regexp' },
28 GlobRef => sub { ref($_) eq 'GLOB' },
35 && $_->isa("IO::Handle")
38 Object => sub { blessed($_) && blessed($_) ne 'Regexp' },
42 return unless defined($_) && length($_);
44 # walk the symbol table tree to avoid autovififying
45 # \*{${main::}{"Foo::"}} == \*main::Foo::
48 foreach my $part (split('::', $_)) {
49 return unless exists ${$$pack}{"${part}::"};
50 $pack = \*{${$$pack}{"${part}::"}};
53 # check for $VERSION or @ISA
54 return 1 if exists ${$$pack}{VERSION}
55 && defined *{${$$pack}{VERSION}}{SCALAR};
56 return 1 if exists ${$$pack}{ISA}
57 && defined *{${$$pack}{ISA}}{ARRAY};
59 # check for any method
60 foreach ( keys %{$$pack} ) {
61 next if substr($_, -2, 2) eq '::';
62 return 1 if defined *{${$$pack}{$_}}{CODE};
77 Mouse::TypeRegistry - simple type constraints
81 =head2 optimized_constraints -> HashRef[CODE]
83 Returns the simple type constraints that Mouse understands.