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