More _generate_class_type_for()
[gitmo/Mouse.git] / lib / Mouse / PurePerl.pm
CommitLineData
df6dd016 1package
2 Mouse::Util;
3
4use strict;
5use warnings;
6
7use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl
8
9use B ();
10
11sub is_class_loaded {
12 my $class = shift;
13
14 return 0 if ref($class) || !defined($class) || !length($class);
15
16 # walk the symbol table tree to avoid autovififying
17 # \*{${main::}{"Foo::"}} == \*main::Foo::
18
19 my $pack = \%::;
20 foreach my $part (split('::', $class)) {
21 my $entry = \$pack->{$part . '::'};
22 return 0 if ref($entry) ne 'GLOB';
23 $pack = *{$entry}{HASH} or return 0;
24 }
25
26 # check for $VERSION or @ISA
27 return 1 if exists $pack->{VERSION}
28 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
29 return 1 if exists $pack->{ISA}
30 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
31
32 # check for any method
33 foreach my $name( keys %{$pack} ) {
34 my $entry = \$pack->{$name};
35 return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
36 }
37
38 # fail
39 return 0;
40}
41
42
43# taken from Sub::Identify
44sub get_code_info {
45 my ($coderef) = @_;
46 ref($coderef) or return;
47
48 my $cv = B::svref_2object($coderef);
49 $cv->isa('B::CV') or return;
50
51 my $gv = $cv->GV;
52 $gv->isa('B::GV') or return;
53
54 return ($gv->STASH->NAME, $gv->NAME);
55}
56
57sub get_code_package{
58 my($coderef) = @_;
59
60 my $cv = B::svref_2object($coderef);
61 $cv->isa('B::CV') or return '';
62
63 my $gv = $cv->GV;
64 $gv->isa('B::GV') or return '';
65
66 return $gv->STASH->NAME;
67}
68
7d96ae4d 69sub get_code_ref{
70 my($package, $name) = @_;
71 no strict 'refs';
72 no warnings 'once';
73 use warnings FATAL => 'uninitialized';
74 return *{$package . '::' . $name}{CODE};
75}
76
77package
78 Mouse::Util::TypeConstraints;
79
1d5ecd5f 80sub _generate_class_type_for{
81 my($for_class, $name) = @_;
82
186fae62 83 my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
1d5ecd5f 84
85 if(defined $name){
86 no strict 'refs';
87 *{ caller() . '::' . $name } = $predicate;
88 return;
89 }
90
91 return $predicate;
92}
93
94
7d96ae4d 95sub Any { 1 }
96sub Item { 1 }
7d96ae4d 97
98sub Bool { $_[0] ? $_[0] eq '1' : 1 }
99sub Undef { !defined($_[0]) }
100sub Defined { defined($_[0]) }
101sub Value { defined($_[0]) && !ref($_[0]) }
102sub Num { !ref($_[0]) && looks_like_number($_[0]) }
103sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
104sub Str { defined($_[0]) && !ref($_[0]) }
105
106sub Ref { ref($_[0]) }
107sub ScalarRef { ref($_[0]) eq 'SCALAR' }
108sub ArrayRef { ref($_[0]) eq 'ARRAY' }
109sub HashRef { ref($_[0]) eq 'HASH' }
110sub CodeRef { ref($_[0]) eq 'CODE' }
111sub RegexpRef { ref($_[0]) eq 'Regexp' }
112sub GlobRef { ref($_[0]) eq 'GLOB' }
113
114sub FileHandle {
115 openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
116}
117
118sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
119
120sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
121sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
122
123
43165725 124package
125 Mouse::Meta::Module;
126
127sub name { $_[0]->{package} }
128
2591e962 129sub namespace{
130 my $name = $_[0]->{package};
131 no strict 'refs';
132 return \%{ $name . '::' };
133}
134
43165725 135package
136 Mouse::Meta::Class;
137
138sub is_anon_class{
139 return exists $_[0]->{anon_serial_id};
140}
141
142sub roles { $_[0]->{roles} }
143
cccb83de 144sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
145
43165725 146package
147 Mouse::Meta::Role;
148
149sub is_anon_role{
150 return exists $_[0]->{anon_serial_id};
151}
152
153sub get_roles { $_[0]->{roles} }
154
155package
156 Mouse::Meta::Attribute;
157
158
159# readers
160
161sub name { $_[0]->{name} }
162sub associated_class { $_[0]->{associated_class} }
163
164sub accessor { $_[0]->{accessor} }
165sub reader { $_[0]->{reader} }
166sub writer { $_[0]->{writer} }
167sub predicate { $_[0]->{predicate} }
168sub clearer { $_[0]->{clearer} }
169sub handles { $_[0]->{handles} }
170
171sub _is_metadata { $_[0]->{is} }
172sub is_required { $_[0]->{required} }
173sub default { $_[0]->{default} }
174sub is_lazy { $_[0]->{lazy} }
175sub is_lazy_build { $_[0]->{lazy_build} }
176sub is_weak_ref { $_[0]->{weak_ref} }
177sub init_arg { $_[0]->{init_arg} }
178sub type_constraint { $_[0]->{type_constraint} }
179
180sub trigger { $_[0]->{trigger} }
181sub builder { $_[0]->{builder} }
182sub should_auto_deref { $_[0]->{auto_deref} }
183sub should_coerce { $_[0]->{coerce} }
184
d899d3e7 185sub documentation { $_[0]->{documentation} }
186
43165725 187# predicates
188
189sub has_accessor { exists $_[0]->{accessor} }
190sub has_reader { exists $_[0]->{reader} }
191sub has_writer { exists $_[0]->{writer} }
192sub has_predicate { exists $_[0]->{predicate} }
193sub has_clearer { exists $_[0]->{clearer} }
194sub has_handles { exists $_[0]->{handles} }
195
196sub has_default { exists $_[0]->{default} }
197sub has_type_constraint { exists $_[0]->{type_constraint} }
198sub has_trigger { exists $_[0]->{trigger} }
199sub has_builder { exists $_[0]->{builder} }
200
d899d3e7 201sub has_documentation { exists $_[0]->{documentation} }
202
93540011 203sub accessor_metaclass(){ 'Mouse::Meta::Method::Accessor' }
204
43165725 205package
206 Mouse::Meta::TypeConstraint;
207
208sub name { $_[0]->{name} }
209sub parent { $_[0]->{parent} }
210sub message { $_[0]->{message} }
211
212sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
213
93540011 214sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
df6dd016 215
93540011 216
217sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
df6dd016 218
2191;
220__END__