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