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