Commit | Line | Data |
ccb38d0b |
1 | package Mouse::PurePerl; |
2 | |
a52cca04 |
3 | require Mouse::Util; |
4 | |
6fffa617 |
5 | package Mouse::Util; |
df6dd016 |
6 | |
7 | use strict; |
8 | use warnings; |
9 | |
10 | use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl |
11 | |
12 | use B (); |
13 | |
0ffc4183 |
14 | |
15 | # taken from Class/MOP.pm |
16 | sub is_valid_class_name { |
17 | my $class = shift; |
18 | |
19 | return 0 if ref($class); |
20 | return 0 unless defined($class); |
21 | |
22 | return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms; |
23 | |
24 | return 0; |
25 | } |
26 | |
df6dd016 |
27 | sub is_class_loaded { |
28 | my $class = shift; |
29 | |
30 | return 0 if ref($class) || !defined($class) || !length($class); |
31 | |
32 | # walk the symbol table tree to avoid autovififying |
3bb9e54e |
33 | # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar:: |
df6dd016 |
34 | |
35 | my $pack = \%::; |
36 | foreach my $part (split('::', $class)) { |
3bb9e54e |
37 | $part .= '::'; |
38 | return 0 if !exists $pack->{$part}; |
39 | |
40 | my $entry = \$pack->{$part}; |
df6dd016 |
41 | return 0 if ref($entry) ne 'GLOB'; |
3bb9e54e |
42 | $pack = *{$entry}{HASH}; |
df6dd016 |
43 | } |
44 | |
3bb9e54e |
45 | return 0 if !%{$pack}; |
46 | |
df6dd016 |
47 | # check for $VERSION or @ISA |
48 | return 1 if exists $pack->{VERSION} |
49 | && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} }; |
50 | return 1 if exists $pack->{ISA} |
51 | && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0; |
52 | |
53 | # check for any method |
54 | foreach my $name( keys %{$pack} ) { |
55 | my $entry = \$pack->{$name}; |
56 | return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE}; |
57 | } |
58 | |
59 | # fail |
60 | return 0; |
61 | } |
62 | |
63 | |
64 | # taken from Sub::Identify |
65 | sub get_code_info { |
66 | my ($coderef) = @_; |
67 | ref($coderef) or return; |
68 | |
69 | my $cv = B::svref_2object($coderef); |
70 | $cv->isa('B::CV') or return; |
71 | |
72 | my $gv = $cv->GV; |
73 | $gv->isa('B::GV') or return; |
74 | |
75 | return ($gv->STASH->NAME, $gv->NAME); |
76 | } |
77 | |
78 | sub get_code_package{ |
79 | my($coderef) = @_; |
80 | |
81 | my $cv = B::svref_2object($coderef); |
82 | $cv->isa('B::CV') or return ''; |
83 | |
84 | my $gv = $cv->GV; |
85 | $gv->isa('B::GV') or return ''; |
86 | |
87 | return $gv->STASH->NAME; |
88 | } |
89 | |
7d96ae4d |
90 | sub get_code_ref{ |
91 | my($package, $name) = @_; |
92 | no strict 'refs'; |
93 | no warnings 'once'; |
94 | use warnings FATAL => 'uninitialized'; |
95 | return *{$package . '::' . $name}{CODE}; |
96 | } |
97 | |
e3540312 |
98 | sub generate_isa_predicate_for { |
1d5ecd5f |
99 | my($for_class, $name) = @_; |
100 | |
f48920c1 |
101 | my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) }; |
1d5ecd5f |
102 | |
103 | if(defined $name){ |
1194aede |
104 | Mouse::Util::install_subroutines(scalar caller, $name => $predicate); |
1d5ecd5f |
105 | return; |
106 | } |
107 | |
108 | return $predicate; |
109 | } |
110 | |
ebe91068 |
111 | sub generate_can_predicate_for { |
112 | my($methods_ref, $name) = @_; |
113 | |
114 | my @methods = @{$methods_ref}; |
115 | |
116 | my $predicate = sub{ |
117 | my($instance) = @_; |
118 | if(Scalar::Util::blessed($instance)){ |
119 | foreach my $method(@methods){ |
120 | if(!$instance->can($method)){ |
121 | return 0; |
122 | } |
123 | } |
124 | return 1; |
125 | } |
126 | return 0; |
127 | }; |
128 | |
129 | if(defined $name){ |
1194aede |
130 | Mouse::Util::install_subroutines(scalar caller, $name => $predicate); |
ebe91068 |
131 | return; |
132 | } |
133 | |
134 | return $predicate; |
135 | } |
1d5ecd5f |
136 | |
6fffa617 |
137 | package Mouse::Util::TypeConstraints; |
f48920c1 |
138 | |
139 | use Scalar::Util qw(blessed looks_like_number openhandle); |
140 | |
7d96ae4d |
141 | sub Any { 1 } |
142 | sub Item { 1 } |
7d96ae4d |
143 | |
144 | sub Bool { $_[0] ? $_[0] eq '1' : 1 } |
145 | sub Undef { !defined($_[0]) } |
146 | sub Defined { defined($_[0]) } |
147 | sub Value { defined($_[0]) && !ref($_[0]) } |
1adf864e |
148 | sub Num { looks_like_number($_[0]) } |
149 | sub Int { |
150 | my($value) = @_; |
0cfe08a4 |
151 | looks_like_number($value) && $value =~ /\A [+-]? [0-9]+ \z/xms; |
1adf864e |
152 | } |
9848d3e1 |
153 | sub Str { |
154 | my($value) = @_; |
155 | return defined($value) && ref(\$value) eq 'SCALAR'; |
156 | } |
7d96ae4d |
157 | |
158 | sub Ref { ref($_[0]) } |
9848d3e1 |
159 | sub ScalarRef { |
160 | my($value) = @_; |
161 | return ref($value) eq 'SCALAR' |
162 | } |
7d96ae4d |
163 | sub ArrayRef { ref($_[0]) eq 'ARRAY' } |
164 | sub HashRef { ref($_[0]) eq 'HASH' } |
165 | sub CodeRef { ref($_[0]) eq 'CODE' } |
166 | sub RegexpRef { ref($_[0]) eq 'Regexp' } |
167 | sub GlobRef { ref($_[0]) eq 'GLOB' } |
168 | |
169 | sub FileHandle { |
9848d3e1 |
170 | return openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle")) |
7d96ae4d |
171 | } |
172 | |
173 | sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' } |
174 | |
175 | sub ClassName { Mouse::Util::is_class_loaded($_[0]) } |
176 | sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') } |
177 | |
619338ac |
178 | sub _parameterize_ArrayRef_for { |
179 | my($type_parameter) = @_; |
180 | my $check = $type_parameter->_compiled_type_constraint; |
181 | |
182 | return sub { |
183 | foreach my $value (@{$_}) { |
184 | return undef unless $check->($value); |
185 | } |
186 | return 1; |
187 | } |
188 | } |
189 | |
190 | sub _parameterize_HashRef_for { |
191 | my($type_parameter) = @_; |
192 | my $check = $type_parameter->_compiled_type_constraint; |
193 | |
194 | return sub { |
195 | foreach my $value(values %{$_}){ |
196 | return undef unless $check->($value); |
197 | } |
198 | return 1; |
199 | }; |
200 | } |
201 | |
202 | # 'Maybe' type accepts 'Any', so it requires parameters |
203 | sub _parameterize_Maybe_for { |
204 | my($type_parameter) = @_; |
205 | my $check = $type_parameter->_compiled_type_constraint; |
206 | |
207 | return sub{ |
208 | return !defined($_) || $check->($_); |
209 | }; |
9b923ac8 |
210 | } |
7d96ae4d |
211 | |
6fffa617 |
212 | package Mouse::Meta::Module; |
43165725 |
213 | |
926404f2 |
214 | sub name { $_[0]->{package} } |
215 | |
216 | sub _method_map { $_[0]->{methods} } |
c5f6ad05 |
217 | sub _attribute_map{ $_[0]->{attributes} } |
43165725 |
218 | |
2591e962 |
219 | sub namespace{ |
220 | my $name = $_[0]->{package}; |
221 | no strict 'refs'; |
222 | return \%{ $name . '::' }; |
223 | } |
224 | |
3e44140b |
225 | sub add_method { |
226 | my($self, $name, $code) = @_; |
227 | |
228 | if(!defined $name){ |
229 | $self->throw_error('You must pass a defined name'); |
230 | } |
231 | if(!defined $code){ |
232 | $self->throw_error('You must pass a defined code'); |
233 | } |
234 | |
235 | if(ref($code) ne 'CODE'){ |
236 | $code = \&{$code}; # coerce |
237 | } |
238 | |
239 | $self->{methods}->{$name} = $code; # Moose stores meta object here. |
240 | |
1194aede |
241 | Mouse::Util::install_subroutines($self->name, |
242 | $name => $code, |
243 | ); |
3e44140b |
244 | return; |
245 | } |
246 | |
6fffa617 |
247 | package Mouse::Meta::Class; |
43165725 |
248 | |
e088c5fc |
249 | use Mouse::Meta::Method::Constructor; |
250 | use Mouse::Meta::Method::Destructor; |
251 | |
e058b279 |
252 | sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' } |
253 | sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' } |
254 | |
255 | sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' } |
256 | sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' } |
4e7e3250 |
257 | |
43165725 |
258 | sub is_anon_class{ |
259 | return exists $_[0]->{anon_serial_id}; |
260 | } |
261 | |
262 | sub roles { $_[0]->{roles} } |
263 | |
cccb83de |
264 | sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } } |
265 | |
da4432f3 |
266 | sub get_all_attributes { |
267 | my($self) = @_; |
047d7af0 |
268 | my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa; |
da4432f3 |
269 | return values %attrs; |
270 | } |
271 | |
ba153b33 |
272 | sub new_object { |
273 | my $self = shift; |
274 | my %args = (@_ == 1 ? %{$_[0]} : @_); |
275 | |
276 | my $object = bless {}, $self->name; |
277 | |
278 | $self->_initialize_object($object, \%args); |
279 | return $object; |
280 | } |
281 | |
4e7e3250 |
282 | sub _initialize_object{ |
283 | my($self, $object, $args, $ignore_triggers) = @_; |
284 | |
285 | my @triggers_queue; |
286 | |
287 | foreach my $attribute ($self->get_all_attributes) { |
288 | my $init_arg = $attribute->init_arg; |
289 | my $slot = $attribute->name; |
290 | |
291 | if (defined($init_arg) && exists($args->{$init_arg})) { |
292 | $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object); |
293 | |
294 | weaken($object->{$slot}) |
295 | if ref($object->{$slot}) && $attribute->is_weak_ref; |
296 | |
297 | if ($attribute->has_trigger) { |
298 | push @triggers_queue, [ $attribute->trigger, $object->{$slot} ]; |
299 | } |
300 | } |
301 | else { # no init arg |
302 | if ($attribute->has_default || $attribute->has_builder) { |
303 | if (!$attribute->is_lazy) { |
304 | my $default = $attribute->default; |
305 | my $builder = $attribute->builder; |
306 | my $value = $builder ? $object->$builder() |
307 | : ref($default) eq 'CODE' ? $object->$default() |
308 | : $default; |
309 | |
310 | $object->{$slot} = $attribute->_coerce_and_verify($value, $object); |
311 | |
312 | weaken($object->{$slot}) |
313 | if ref($object->{$slot}) && $attribute->is_weak_ref; |
314 | } |
315 | } |
316 | elsif($attribute->is_required) { |
317 | $self->throw_error("Attribute (".$attribute->name.") is required"); |
318 | } |
319 | } |
320 | } |
321 | |
322 | if(!$ignore_triggers){ |
323 | foreach my $trigger_and_value(@triggers_queue){ |
324 | my($trigger, $value) = @{$trigger_and_value}; |
325 | $trigger->($object, $value); |
326 | } |
327 | } |
328 | |
329 | if($self->is_anon_class){ |
330 | $object->{__METACLASS__} = $self; |
331 | } |
332 | |
333 | return; |
334 | } |
335 | |
80efe911 |
336 | sub is_immutable { $_[0]->{is_immutable} } |
4e7e3250 |
337 | |
e128626c |
338 | sub __strict_constructor{ $_[0]->{strict_constructor} } |
339 | |
6fffa617 |
340 | package Mouse::Meta::Role; |
43165725 |
341 | |
e058b279 |
342 | sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' } |
343 | |
43165725 |
344 | sub is_anon_role{ |
345 | return exists $_[0]->{anon_serial_id}; |
346 | } |
347 | |
348 | sub get_roles { $_[0]->{roles} } |
349 | |
cb60d0b5 |
350 | sub add_before_method_modifier { |
351 | my ($self, $method_name, $method) = @_; |
352 | |
353 | push @{ $self->{before_method_modifiers}{$method_name} ||= [] }, $method; |
354 | return; |
355 | } |
356 | sub add_around_method_modifier { |
357 | my ($self, $method_name, $method) = @_; |
358 | |
359 | push @{ $self->{around_method_modifiers}{$method_name} ||= [] }, $method; |
360 | return; |
361 | } |
362 | sub add_after_method_modifier { |
363 | my ($self, $method_name, $method) = @_; |
364 | |
365 | push @{ $self->{after_method_modifiers}{$method_name} ||= [] }, $method; |
366 | return; |
367 | } |
368 | |
369 | sub get_before_method_modifiers { |
370 | my ($self, $method_name) = @_; |
371 | return @{ $self->{before_method_modifiers}{$method_name} ||= [] } |
372 | } |
373 | sub get_around_method_modifiers { |
374 | my ($self, $method_name) = @_; |
375 | return @{ $self->{around_method_modifiers}{$method_name} ||= [] } |
376 | } |
377 | sub get_after_method_modifiers { |
378 | my ($self, $method_name) = @_; |
379 | return @{ $self->{after_method_modifiers}{$method_name} ||= [] } |
380 | } |
381 | |
6fffa617 |
382 | package Mouse::Meta::Attribute; |
43165725 |
383 | |
e058b279 |
384 | require Mouse::Meta::Method::Accessor; |
385 | |
386 | sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' } |
43165725 |
387 | |
388 | # readers |
389 | |
390 | sub name { $_[0]->{name} } |
391 | sub associated_class { $_[0]->{associated_class} } |
392 | |
393 | sub accessor { $_[0]->{accessor} } |
394 | sub reader { $_[0]->{reader} } |
395 | sub writer { $_[0]->{writer} } |
396 | sub predicate { $_[0]->{predicate} } |
397 | sub clearer { $_[0]->{clearer} } |
398 | sub handles { $_[0]->{handles} } |
399 | |
400 | sub _is_metadata { $_[0]->{is} } |
401 | sub is_required { $_[0]->{required} } |
402 | sub default { $_[0]->{default} } |
403 | sub is_lazy { $_[0]->{lazy} } |
404 | sub is_lazy_build { $_[0]->{lazy_build} } |
405 | sub is_weak_ref { $_[0]->{weak_ref} } |
406 | sub init_arg { $_[0]->{init_arg} } |
407 | sub type_constraint { $_[0]->{type_constraint} } |
408 | |
409 | sub trigger { $_[0]->{trigger} } |
410 | sub builder { $_[0]->{builder} } |
411 | sub should_auto_deref { $_[0]->{auto_deref} } |
412 | sub should_coerce { $_[0]->{coerce} } |
413 | |
d899d3e7 |
414 | sub documentation { $_[0]->{documentation} } |
415 | |
43165725 |
416 | # predicates |
417 | |
418 | sub has_accessor { exists $_[0]->{accessor} } |
419 | sub has_reader { exists $_[0]->{reader} } |
420 | sub has_writer { exists $_[0]->{writer} } |
421 | sub has_predicate { exists $_[0]->{predicate} } |
422 | sub has_clearer { exists $_[0]->{clearer} } |
423 | sub has_handles { exists $_[0]->{handles} } |
424 | |
425 | sub has_default { exists $_[0]->{default} } |
426 | sub has_type_constraint { exists $_[0]->{type_constraint} } |
427 | sub has_trigger { exists $_[0]->{trigger} } |
428 | sub has_builder { exists $_[0]->{builder} } |
429 | |
d899d3e7 |
430 | sub has_documentation { exists $_[0]->{documentation} } |
431 | |
ba1f50a2 |
432 | sub _process_options{ |
433 | my($class, $name, $args) = @_; |
434 | |
435 | # taken from Class::MOP::Attribute::new |
436 | |
437 | defined($name) |
438 | or $class->throw_error('You must provide a name for the attribute'); |
439 | |
440 | if(!exists $args->{init_arg}){ |
441 | $args->{init_arg} = $name; |
442 | } |
443 | |
444 | # 'required' requires eigher 'init_arg', 'builder', or 'default' |
445 | my $can_be_required = defined( $args->{init_arg} ); |
446 | |
447 | if(exists $args->{builder}){ |
448 | # XXX: |
449 | # Moose refuses a CODE ref builder, but Mouse doesn't for backward compatibility |
450 | # This feature will be changed in a future. (gfx) |
451 | $class->throw_error('builder must be a defined scalar value which is a method name') |
452 | #if ref $args->{builder} || !defined $args->{builder}; |
453 | if !defined $args->{builder}; |
454 | |
455 | $can_be_required++; |
456 | } |
457 | elsif(exists $args->{default}){ |
458 | if(ref $args->{default} && ref($args->{default}) ne 'CODE'){ |
459 | $class->throw_error("References are not allowed as default values, you must " |
460 | . "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"); |
461 | } |
462 | $can_be_required++; |
463 | } |
464 | |
465 | if( $args->{required} && !$can_be_required ) { |
466 | $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg"); |
467 | } |
468 | |
469 | # taken from Mouse::Meta::Attribute->new and ->_process_args |
470 | |
471 | if(exists $args->{is}){ |
472 | my $is = $args->{is}; |
473 | |
474 | if($is eq 'ro'){ |
475 | $args->{reader} ||= $name; |
476 | } |
477 | elsif($is eq 'rw'){ |
478 | if(exists $args->{writer}){ |
479 | $args->{reader} ||= $name; |
480 | } |
481 | else{ |
482 | $args->{accessor} ||= $name; |
483 | } |
484 | } |
485 | elsif($is eq 'bare'){ |
486 | # do nothing, but don't complain (later) about missing methods |
487 | } |
488 | else{ |
489 | $is = 'undef' if !defined $is; |
490 | $class->throw_error("I do not understand this option (is => $is) on attribute ($name)"); |
491 | } |
492 | } |
493 | |
494 | my $tc; |
495 | if(exists $args->{isa}){ |
d503a4f3 |
496 | $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($args->{isa}); |
ba1f50a2 |
497 | } |
d503a4f3 |
498 | |
499 | if(exists $args->{does}){ |
500 | if(defined $tc){ # both isa and does supplied |
501 | my $does_ok = do{ |
502 | local $@; |
503 | eval{ "$tc"->does($args) }; |
504 | }; |
505 | if(!$does_ok){ |
506 | $class->throw_error("Cannot have both an isa option and a does option because '$tc' does not do '$args->{does}' on attribute ($name)"); |
507 | } |
508 | } |
509 | else { |
510 | $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_does_type_constraint($args->{does}); |
511 | } |
ba1f50a2 |
512 | } |
ba1f50a2 |
513 | |
514 | if($args->{coerce}){ |
515 | defined($tc) |
516 | || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)"); |
517 | |
518 | $args->{weak_ref} |
519 | && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)"); |
520 | } |
521 | |
522 | if ($args->{lazy_build}) { |
523 | exists($args->{default}) |
524 | && $class->throw_error("You can not use lazy_build and default for the same attribute ($name)"); |
525 | |
526 | $args->{lazy} = 1; |
527 | $args->{builder} ||= "_build_${name}"; |
528 | if ($name =~ /^_/) { |
529 | $args->{clearer} ||= "_clear${name}"; |
530 | $args->{predicate} ||= "_has${name}"; |
531 | } |
532 | else { |
533 | $args->{clearer} ||= "clear_${name}"; |
534 | $args->{predicate} ||= "has_${name}"; |
535 | } |
536 | } |
537 | |
538 | if ($args->{auto_deref}) { |
539 | defined($tc) |
540 | || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)"); |
541 | |
542 | ( $tc->is_a_type_of('ArrayRef') || $tc->is_a_type_of('HashRef') ) |
543 | || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)"); |
544 | } |
545 | |
546 | if (exists $args->{trigger}) { |
547 | ('CODE' eq ref $args->{trigger}) |
548 | || $class->throw_error("Trigger must be a CODE ref on attribute ($name)"); |
549 | } |
550 | |
551 | if ($args->{lazy}) { |
552 | (exists $args->{default} || defined $args->{builder}) |
553 | || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it"); |
554 | } |
555 | |
556 | return; |
557 | } |
558 | |
559 | |
6fffa617 |
560 | package Mouse::Meta::TypeConstraint; |
43165725 |
561 | |
562 | sub name { $_[0]->{name} } |
563 | sub parent { $_[0]->{parent} } |
564 | sub message { $_[0]->{message} } |
565 | |
72dc2c9d |
566 | sub type_parameter { $_[0]->{type_parameter} } |
43165725 |
567 | sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} } |
93540011 |
568 | sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} } |
df6dd016 |
569 | |
72dc2c9d |
570 | sub __is_parameterized { exists $_[0]->{type_parameter} } |
571 | sub has_coercion { exists $_[0]->{_compiled_type_coercion} } |
df6dd016 |
572 | |
f790c46b |
573 | |
574 | sub compile_type_constraint{ |
575 | my($self) = @_; |
576 | |
577 | # add parents first |
578 | my @checks; |
579 | for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){ |
580 | if($parent->{hand_optimized_type_constraint}){ |
581 | unshift @checks, $parent->{hand_optimized_type_constraint}; |
582 | last; # a hand optimized constraint must include all the parents |
583 | } |
584 | elsif($parent->{constraint}){ |
585 | unshift @checks, $parent->{constraint}; |
586 | } |
587 | } |
588 | |
589 | # then add child |
590 | if($self->{constraint}){ |
591 | push @checks, $self->{constraint}; |
592 | } |
593 | |
594 | if($self->{type_constraints}){ # Union |
595 | my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} }; |
596 | push @checks, sub{ |
597 | foreach my $c(@types){ |
598 | return 1 if $c->($_[0]); |
599 | } |
600 | return 0; |
601 | }; |
602 | } |
603 | |
604 | if(@checks == 0){ |
605 | $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any; |
606 | } |
607 | else{ |
608 | $self->{compiled_type_constraint} = sub{ |
609 | my(@args) = @_; |
610 | local $_ = $args[0]; |
611 | foreach my $c(@checks){ |
612 | return undef if !$c->(@args); |
613 | } |
614 | return 1; |
615 | }; |
616 | } |
617 | return; |
618 | } |
619 | |
6fffa617 |
620 | package Mouse::Object; |
aa2d2e2c |
621 | |
aa2d2e2c |
622 | sub BUILDARGS { |
623 | my $class = shift; |
f790c46b |
624 | |
aa2d2e2c |
625 | if (scalar @_ == 1) { |
626 | (ref($_[0]) eq 'HASH') |
627 | || $class->meta->throw_error("Single parameters to new() must be a HASH ref"); |
628 | |
629 | return {%{$_[0]}}; |
630 | } |
631 | else { |
632 | return {@_}; |
633 | } |
634 | } |
f790c46b |
635 | |
af04626d |
636 | sub new { |
637 | my $class = shift; |
638 | |
639 | $class->meta->throw_error('Cannot call new() on an instance') if ref $class; |
640 | |
641 | my $args = $class->BUILDARGS(@_); |
642 | |
643 | my $meta = Mouse::Meta::Class->initialize($class); |
644 | my $self = $meta->new_object($args); |
645 | |
646 | # BUILDALL |
647 | if( $self->can('BUILD') ) { |
648 | for my $class (reverse $meta->linearized_isa) { |
649 | my $build = Mouse::Util::get_code_ref($class, 'BUILD') |
650 | || next; |
651 | |
652 | $self->$build($args); |
653 | } |
654 | } |
655 | |
656 | return $self; |
657 | } |
658 | |
a5c683f6 |
659 | sub DESTROY { |
660 | my $self = shift; |
661 | |
662 | return unless $self->can('DEMOLISH'); # short circuit |
663 | |
664 | local $?; |
665 | |
666 | my $e = do{ |
667 | local $@; |
668 | eval{ |
a5c683f6 |
669 | # DEMOLISHALL |
670 | |
671 | # We cannot count on being able to retrieve a previously made |
672 | # metaclass, _or_ being able to make a new one during global |
673 | # destruction. However, we should still be able to use mro at |
674 | # that time (at least tests suggest so ;) |
675 | |
676 | foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) { |
677 | my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH') |
678 | || next; |
679 | |
6514735e |
680 | $self->$demolish($Mouse::Util::in_global_destruction); |
a5c683f6 |
681 | } |
682 | }; |
683 | $@; |
684 | }; |
685 | |
686 | no warnings 'misc'; |
687 | die $e if $e; # rethrow |
688 | } |
689 | |
adb5eb76 |
690 | sub BUILDALL { |
691 | my $self = shift; |
692 | |
693 | # short circuit |
694 | return unless $self->can('BUILD'); |
695 | |
696 | for my $class (reverse $self->meta->linearized_isa) { |
697 | my $build = Mouse::Util::get_code_ref($class, 'BUILD') |
698 | || next; |
699 | |
700 | $self->$build(@_); |
701 | } |
702 | return; |
703 | } |
704 | |
705 | sub DEMOLISHALL; |
706 | *DEMOLISHALL = \&DESTROY; |
707 | |
df6dd016 |
708 | 1; |
709 | __END__ |
ccb38d0b |
710 | |
711 | =head1 NAME |
712 | |
713 | Mouse::PurePerl - A Mouse guts in pure Perl |
714 | |
715 | =head1 VERSION |
716 | |
123fd4f8 |
717 | This document describes Mouse version 0.50_09 |
ccb38d0b |
718 | |
719 | =head1 SEE ALSO |
720 | |
721 | L<Mouse::XS> |
722 | |
723 | =cut |