Commit | Line | Data |
ccb38d0b |
1 | package Mouse::PurePerl; |
2 | |
a52cca04 |
3 | require Mouse::Util; |
4 | |
df6dd016 |
5 | package |
6 | Mouse::Util; |
7 | |
8 | use strict; |
9 | use warnings; |
10 | |
11 | use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl |
12 | |
13 | use B (); |
14 | |
15 | sub is_class_loaded { |
16 | my $class = shift; |
17 | |
18 | return 0 if ref($class) || !defined($class) || !length($class); |
19 | |
20 | # walk the symbol table tree to avoid autovififying |
21 | # \*{${main::}{"Foo::"}} == \*main::Foo:: |
22 | |
23 | my $pack = \%::; |
24 | foreach my $part (split('::', $class)) { |
25 | my $entry = \$pack->{$part . '::'}; |
26 | return 0 if ref($entry) ne 'GLOB'; |
27 | $pack = *{$entry}{HASH} or return 0; |
28 | } |
29 | |
30 | # check for $VERSION or @ISA |
31 | return 1 if exists $pack->{VERSION} |
32 | && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} }; |
33 | return 1 if exists $pack->{ISA} |
34 | && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0; |
35 | |
36 | # check for any method |
37 | foreach my $name( keys %{$pack} ) { |
38 | my $entry = \$pack->{$name}; |
39 | return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE}; |
40 | } |
41 | |
42 | # fail |
43 | return 0; |
44 | } |
45 | |
46 | |
47 | # taken from Sub::Identify |
48 | sub get_code_info { |
49 | my ($coderef) = @_; |
50 | ref($coderef) or return; |
51 | |
52 | my $cv = B::svref_2object($coderef); |
53 | $cv->isa('B::CV') or return; |
54 | |
55 | my $gv = $cv->GV; |
56 | $gv->isa('B::GV') or return; |
57 | |
58 | return ($gv->STASH->NAME, $gv->NAME); |
59 | } |
60 | |
61 | sub get_code_package{ |
62 | my($coderef) = @_; |
63 | |
64 | my $cv = B::svref_2object($coderef); |
65 | $cv->isa('B::CV') or return ''; |
66 | |
67 | my $gv = $cv->GV; |
68 | $gv->isa('B::GV') or return ''; |
69 | |
70 | return $gv->STASH->NAME; |
71 | } |
72 | |
7d96ae4d |
73 | sub get_code_ref{ |
74 | my($package, $name) = @_; |
75 | no strict 'refs'; |
76 | no warnings 'once'; |
77 | use warnings FATAL => 'uninitialized'; |
78 | return *{$package . '::' . $name}{CODE}; |
79 | } |
80 | |
e3540312 |
81 | sub generate_isa_predicate_for { |
1d5ecd5f |
82 | my($for_class, $name) = @_; |
83 | |
f48920c1 |
84 | my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) }; |
1d5ecd5f |
85 | |
86 | if(defined $name){ |
87 | no strict 'refs'; |
88 | *{ caller() . '::' . $name } = $predicate; |
89 | return; |
90 | } |
91 | |
92 | return $predicate; |
93 | } |
94 | |
95 | |
f48920c1 |
96 | package |
97 | Mouse::Util::TypeConstraints; |
98 | |
99 | use Scalar::Util qw(blessed looks_like_number openhandle); |
100 | |
7d96ae4d |
101 | sub Any { 1 } |
102 | sub Item { 1 } |
7d96ae4d |
103 | |
104 | sub Bool { $_[0] ? $_[0] eq '1' : 1 } |
105 | sub Undef { !defined($_[0]) } |
106 | sub Defined { defined($_[0]) } |
107 | sub Value { defined($_[0]) && !ref($_[0]) } |
108 | sub Num { !ref($_[0]) && looks_like_number($_[0]) } |
109 | sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ } |
110 | sub Str { defined($_[0]) && !ref($_[0]) } |
111 | |
112 | sub Ref { ref($_[0]) } |
113 | sub ScalarRef { ref($_[0]) eq 'SCALAR' } |
114 | sub ArrayRef { ref($_[0]) eq 'ARRAY' } |
115 | sub HashRef { ref($_[0]) eq 'HASH' } |
116 | sub CodeRef { ref($_[0]) eq 'CODE' } |
117 | sub RegexpRef { ref($_[0]) eq 'Regexp' } |
118 | sub GlobRef { ref($_[0]) eq 'GLOB' } |
119 | |
120 | sub FileHandle { |
121 | openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle")) |
122 | } |
123 | |
124 | sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' } |
125 | |
126 | sub ClassName { Mouse::Util::is_class_loaded($_[0]) } |
127 | sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') } |
128 | |
619338ac |
129 | sub _parameterize_ArrayRef_for { |
130 | my($type_parameter) = @_; |
131 | my $check = $type_parameter->_compiled_type_constraint; |
132 | |
133 | return sub { |
134 | foreach my $value (@{$_}) { |
135 | return undef unless $check->($value); |
136 | } |
137 | return 1; |
138 | } |
139 | } |
140 | |
141 | sub _parameterize_HashRef_for { |
142 | my($type_parameter) = @_; |
143 | my $check = $type_parameter->_compiled_type_constraint; |
144 | |
145 | return sub { |
146 | foreach my $value(values %{$_}){ |
147 | return undef unless $check->($value); |
148 | } |
149 | return 1; |
150 | }; |
151 | } |
152 | |
153 | # 'Maybe' type accepts 'Any', so it requires parameters |
154 | sub _parameterize_Maybe_for { |
155 | my($type_parameter) = @_; |
156 | my $check = $type_parameter->_compiled_type_constraint; |
157 | |
158 | return sub{ |
159 | return !defined($_) || $check->($_); |
160 | }; |
161 | }; |
162 | |
163 | |
7d96ae4d |
164 | |
43165725 |
165 | package |
166 | Mouse::Meta::Module; |
167 | |
926404f2 |
168 | sub name { $_[0]->{package} } |
169 | |
170 | sub _method_map { $_[0]->{methods} } |
c5f6ad05 |
171 | sub _attribute_map{ $_[0]->{attributes} } |
43165725 |
172 | |
2591e962 |
173 | sub namespace{ |
174 | my $name = $_[0]->{package}; |
175 | no strict 'refs'; |
176 | return \%{ $name . '::' }; |
177 | } |
178 | |
3e44140b |
179 | sub add_method { |
180 | my($self, $name, $code) = @_; |
181 | |
182 | if(!defined $name){ |
183 | $self->throw_error('You must pass a defined name'); |
184 | } |
185 | if(!defined $code){ |
186 | $self->throw_error('You must pass a defined code'); |
187 | } |
188 | |
189 | if(ref($code) ne 'CODE'){ |
190 | $code = \&{$code}; # coerce |
191 | } |
192 | |
193 | $self->{methods}->{$name} = $code; # Moose stores meta object here. |
194 | |
195 | my $pkg = $self->name; |
196 | no strict 'refs'; |
197 | no warnings 'redefine', 'once'; |
198 | *{ $pkg . '::' . $name } = $code; |
199 | return; |
200 | } |
201 | |
202 | |
43165725 |
203 | package |
204 | Mouse::Meta::Class; |
205 | |
4e7e3250 |
206 | |
43165725 |
207 | sub is_anon_class{ |
208 | return exists $_[0]->{anon_serial_id}; |
209 | } |
210 | |
211 | sub roles { $_[0]->{roles} } |
212 | |
cccb83de |
213 | sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } } |
214 | |
da4432f3 |
215 | sub get_all_attributes { |
216 | my($self) = @_; |
047d7af0 |
217 | my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa; |
da4432f3 |
218 | return values %attrs; |
219 | } |
220 | |
4e7e3250 |
221 | sub _initialize_object{ |
222 | my($self, $object, $args, $ignore_triggers) = @_; |
223 | |
224 | my @triggers_queue; |
225 | |
226 | foreach my $attribute ($self->get_all_attributes) { |
227 | my $init_arg = $attribute->init_arg; |
228 | my $slot = $attribute->name; |
229 | |
230 | if (defined($init_arg) && exists($args->{$init_arg})) { |
231 | $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object); |
232 | |
233 | weaken($object->{$slot}) |
234 | if ref($object->{$slot}) && $attribute->is_weak_ref; |
235 | |
236 | if ($attribute->has_trigger) { |
237 | push @triggers_queue, [ $attribute->trigger, $object->{$slot} ]; |
238 | } |
239 | } |
240 | else { # no init arg |
241 | if ($attribute->has_default || $attribute->has_builder) { |
242 | if (!$attribute->is_lazy) { |
243 | my $default = $attribute->default; |
244 | my $builder = $attribute->builder; |
245 | my $value = $builder ? $object->$builder() |
246 | : ref($default) eq 'CODE' ? $object->$default() |
247 | : $default; |
248 | |
249 | $object->{$slot} = $attribute->_coerce_and_verify($value, $object); |
250 | |
251 | weaken($object->{$slot}) |
252 | if ref($object->{$slot}) && $attribute->is_weak_ref; |
253 | } |
254 | } |
255 | elsif($attribute->is_required) { |
256 | $self->throw_error("Attribute (".$attribute->name.") is required"); |
257 | } |
258 | } |
259 | } |
260 | |
261 | if(!$ignore_triggers){ |
262 | foreach my $trigger_and_value(@triggers_queue){ |
263 | my($trigger, $value) = @{$trigger_and_value}; |
264 | $trigger->($object, $value); |
265 | } |
266 | } |
267 | |
268 | if($self->is_anon_class){ |
269 | $object->{__METACLASS__} = $self; |
270 | } |
271 | |
272 | return; |
273 | } |
274 | |
275 | |
43165725 |
276 | package |
277 | Mouse::Meta::Role; |
278 | |
279 | sub is_anon_role{ |
280 | return exists $_[0]->{anon_serial_id}; |
281 | } |
282 | |
283 | sub get_roles { $_[0]->{roles} } |
284 | |
285 | package |
286 | Mouse::Meta::Attribute; |
287 | |
d7d8d49b |
288 | use Mouse::Meta::Method::Accessor; |
43165725 |
289 | |
290 | # readers |
291 | |
292 | sub name { $_[0]->{name} } |
293 | sub associated_class { $_[0]->{associated_class} } |
294 | |
295 | sub accessor { $_[0]->{accessor} } |
296 | sub reader { $_[0]->{reader} } |
297 | sub writer { $_[0]->{writer} } |
298 | sub predicate { $_[0]->{predicate} } |
299 | sub clearer { $_[0]->{clearer} } |
300 | sub handles { $_[0]->{handles} } |
301 | |
302 | sub _is_metadata { $_[0]->{is} } |
303 | sub is_required { $_[0]->{required} } |
304 | sub default { $_[0]->{default} } |
305 | sub is_lazy { $_[0]->{lazy} } |
306 | sub is_lazy_build { $_[0]->{lazy_build} } |
307 | sub is_weak_ref { $_[0]->{weak_ref} } |
308 | sub init_arg { $_[0]->{init_arg} } |
309 | sub type_constraint { $_[0]->{type_constraint} } |
310 | |
311 | sub trigger { $_[0]->{trigger} } |
312 | sub builder { $_[0]->{builder} } |
313 | sub should_auto_deref { $_[0]->{auto_deref} } |
314 | sub should_coerce { $_[0]->{coerce} } |
315 | |
d899d3e7 |
316 | sub documentation { $_[0]->{documentation} } |
317 | |
43165725 |
318 | # predicates |
319 | |
320 | sub has_accessor { exists $_[0]->{accessor} } |
321 | sub has_reader { exists $_[0]->{reader} } |
322 | sub has_writer { exists $_[0]->{writer} } |
323 | sub has_predicate { exists $_[0]->{predicate} } |
324 | sub has_clearer { exists $_[0]->{clearer} } |
325 | sub has_handles { exists $_[0]->{handles} } |
326 | |
327 | sub has_default { exists $_[0]->{default} } |
328 | sub has_type_constraint { exists $_[0]->{type_constraint} } |
329 | sub has_trigger { exists $_[0]->{trigger} } |
330 | sub has_builder { exists $_[0]->{builder} } |
331 | |
d899d3e7 |
332 | sub has_documentation { exists $_[0]->{documentation} } |
333 | |
93540011 |
334 | sub accessor_metaclass(){ 'Mouse::Meta::Method::Accessor' } |
335 | |
43165725 |
336 | package |
337 | Mouse::Meta::TypeConstraint; |
338 | |
339 | sub name { $_[0]->{name} } |
340 | sub parent { $_[0]->{parent} } |
341 | sub message { $_[0]->{message} } |
342 | |
343 | sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} } |
344 | |
93540011 |
345 | sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} } |
df6dd016 |
346 | |
93540011 |
347 | sub has_coercion{ exists $_[0]->{_compiled_type_coercion} } |
df6dd016 |
348 | |
f790c46b |
349 | |
350 | sub compile_type_constraint{ |
351 | my($self) = @_; |
352 | |
353 | # add parents first |
354 | my @checks; |
355 | for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){ |
356 | if($parent->{hand_optimized_type_constraint}){ |
357 | unshift @checks, $parent->{hand_optimized_type_constraint}; |
358 | last; # a hand optimized constraint must include all the parents |
359 | } |
360 | elsif($parent->{constraint}){ |
361 | unshift @checks, $parent->{constraint}; |
362 | } |
363 | } |
364 | |
365 | # then add child |
366 | if($self->{constraint}){ |
367 | push @checks, $self->{constraint}; |
368 | } |
369 | |
370 | if($self->{type_constraints}){ # Union |
371 | my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} }; |
372 | push @checks, sub{ |
373 | foreach my $c(@types){ |
374 | return 1 if $c->($_[0]); |
375 | } |
376 | return 0; |
377 | }; |
378 | } |
379 | |
380 | if(@checks == 0){ |
381 | $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any; |
382 | } |
383 | else{ |
384 | $self->{compiled_type_constraint} = sub{ |
385 | my(@args) = @_; |
386 | local $_ = $args[0]; |
387 | foreach my $c(@checks){ |
388 | return undef if !$c->(@args); |
389 | } |
390 | return 1; |
391 | }; |
392 | } |
393 | return; |
394 | } |
395 | |
aa2d2e2c |
396 | package |
397 | Mouse::Object; |
398 | |
399 | |
400 | sub BUILDARGS { |
401 | my $class = shift; |
f790c46b |
402 | |
aa2d2e2c |
403 | if (scalar @_ == 1) { |
404 | (ref($_[0]) eq 'HASH') |
405 | || $class->meta->throw_error("Single parameters to new() must be a HASH ref"); |
406 | |
407 | return {%{$_[0]}}; |
408 | } |
409 | else { |
410 | return {@_}; |
411 | } |
412 | } |
f790c46b |
413 | |
df6dd016 |
414 | 1; |
415 | __END__ |
ccb38d0b |
416 | |
417 | =head1 NAME |
418 | |
419 | Mouse::PurePerl - A Mouse guts in pure Perl |
420 | |
421 | =head1 VERSION |
422 | |
6e168432 |
423 | This document describes Mouse version 0.40_05 |
ccb38d0b |
424 | |
425 | =head1 SEE ALSO |
426 | |
427 | L<Mouse::XS> |
428 | |
429 | =cut |