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