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