1 package Mouse::PurePerl;
2 # The pure Perl backend for Mouse
6 use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl twice
13 # taken from Class/MOP.pm
14 sub is_valid_class_name {
17 return 0 if ref($class);
18 return 0 unless defined($class);
20 return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms;
28 return 0 if ref($class) || !defined($class) || !length($class);
30 # walk the symbol table tree to avoid autovififying
31 # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::
34 foreach my $part (split('::', $class)) {
36 return 0 if !exists $pack->{$part};
38 my $entry = \$pack->{$part};
39 return 0 if ref($entry) ne 'GLOB';
40 $pack = *{$entry}{HASH};
43 return 0 if !%{$pack};
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;
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};
62 # taken from Sub::Identify
65 ref($coderef) or return;
67 my $cv = B::svref_2object($coderef);
68 $cv->isa('B::CV') or return;
71 $gv->isa('B::GV') or return;
73 return ($gv->STASH->NAME, $gv->NAME);
79 my $cv = B::svref_2object($coderef);
80 $cv->isa('B::CV') or return '';
83 $gv->isa('B::GV') or return '';
85 return $gv->STASH->NAME;
89 my($package, $name) = @_;
92 use warnings FATAL => 'uninitialized';
93 return *{$package . '::' . $name}{CODE};
96 sub generate_isa_predicate_for {
97 my($for_class, $name) = @_;
99 my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
102 Mouse::Util::install_subroutines(scalar caller, $name => $predicate);
109 sub generate_can_predicate_for {
110 my($methods_ref, $name) = @_;
112 my @methods = @{$methods_ref};
116 if(Scalar::Util::blessed($instance)){
117 foreach my $method(@methods){
118 if(!$instance->can($method)){
128 Mouse::Util::install_subroutines(scalar caller, $name => $predicate);
135 package Mouse::Util::TypeConstraints;
137 use Scalar::Util qw(blessed looks_like_number openhandle);
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]) }
146 sub Num { looks_like_number($_[0]) }
149 looks_like_number($value) && $value =~ /\A [+-]? [0-9]+ \z/xms;
153 return defined($value) && ref(\$value) eq 'SCALAR';
156 sub Ref { ref($_[0]) }
159 return ref($value) eq 'SCALAR'
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' }
168 return openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
171 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
173 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
174 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
176 sub _parameterize_ArrayRef_for {
177 my($type_parameter) = @_;
178 my $check = $type_parameter->_compiled_type_constraint;
181 foreach my $value (@{$_}) {
182 return undef unless $check->($value);
188 sub _parameterize_HashRef_for {
189 my($type_parameter) = @_;
190 my $check = $type_parameter->_compiled_type_constraint;
193 foreach my $value(values %{$_}){
194 return undef unless $check->($value);
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;
206 return !defined($_) || $check->($_);
210 package Mouse::Meta::Module;
212 sub name { $_[0]->{package} }
214 sub _method_map { $_[0]->{methods} }
215 sub _attribute_map{ $_[0]->{attributes} }
218 my $name = $_[0]->{package};
220 return \%{ $name . '::' };
224 my($self, $name, $code) = @_;
227 $self->throw_error('You must pass a defined name');
230 $self->throw_error('You must pass a defined code');
233 if(ref($code) ne 'CODE'){
234 $code = \&{$code}; # coerce
237 $self->{methods}->{$name} = $code; # Moose stores meta object here.
239 Mouse::Util::install_subroutines($self->name,
245 my $generate_class_accessor = sub {
250 return $self->{$name} = shift;
253 foreach my $class($self->linearized_isa) {
254 my $meta = Mouse::Util::get_metaclass_by_name($class)
257 if(exists $meta->{$name}) {
258 return $meta->{$name};
266 package Mouse::Meta::Class;
268 use Mouse::Meta::Method::Constructor;
269 use Mouse::Meta::Method::Destructor;
271 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
272 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
274 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
275 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
278 return exists $_[0]->{anon_serial_id};
281 sub roles { $_[0]->{roles} }
283 sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
285 sub get_all_attributes {
287 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
288 return values %attrs;
293 my %args = (@_ == 1 ? %{$_[0]} : @_);
295 my $object = bless {}, $meta->name;
297 $meta->_initialize_object($object, \%args);
299 if( $object->can('BUILD') ) {
300 for my $class (reverse $meta->linearized_isa) {
301 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
304 $object->$build(\%args);
313 my $args = $object->Mouse::Object::BUILDARGS(@_);
315 (blessed($object) && $object->isa($class->name))
316 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
318 my $cloned = bless { %$object }, ref $object;
319 $class->_initialize_object($cloned, $args, 1);
324 sub _initialize_object{
325 my($self, $object, $args, $is_cloning) = @_;
331 foreach my $attribute ($self->get_all_attributes) {
332 my $init_arg = $attribute->init_arg;
333 my $slot = $attribute->name;
335 if (defined($init_arg) && exists($args->{$init_arg})) {
336 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
338 weaken($object->{$slot})
339 if ref($object->{$slot}) && $attribute->is_weak_ref;
341 if ($attribute->has_trigger) {
342 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
347 if ($attribute->has_default || $attribute->has_builder) {
348 if (!$attribute->is_lazy && !exists $object->{$slot}) {
349 my $default = $attribute->default;
350 my $builder = $attribute->builder;
351 my $value = $builder ? $object->$builder()
352 : ref($default) eq 'CODE' ? $object->$default()
355 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
357 weaken($object->{$slot})
358 if ref($object->{$slot}) && $attribute->is_weak_ref;
361 elsif(!$is_cloning && $attribute->is_required) {
362 $self->throw_error("Attribute (".$attribute->name.") is required");
367 if($used < keys %{$args} && $self->strict_constructor) {
368 $self->_report_unknown_args([ $self->get_all_attributes ], $args);
372 foreach my $trigger_and_value(@triggers_queue){
373 my($trigger, $value) = @{$trigger_and_value};
374 $trigger->($object, $value);
378 if($self->is_anon_class){
379 $object->{__METACLASS__} = $self;
385 sub is_immutable { $_[0]->{is_immutable} }
387 sub strict_constructor;
388 *strict_constructor = $generate_class_accessor->('strict_constructor');
390 sub _report_unknown_args {
391 my($metaclass, $attrs, $args) = @_;
395 foreach my $attr(@{$attrs}){
396 my $init_arg = $attr->init_arg;
397 if(defined $init_arg){
398 $init_args{$init_arg}++;
402 while(my $key = each %{$args}){
403 if(!exists $init_args{$key}){
404 push @unknowns, $key;
408 $metaclass->throw_error( sprintf
409 "Unknown attribute passed to the constructor of %s: %s",
410 $metaclass->name, Mouse::Util::english_list(@unknowns),
414 package Mouse::Meta::Role;
416 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
419 return exists $_[0]->{anon_serial_id};
422 sub get_roles { $_[0]->{roles} }
424 sub add_before_method_modifier {
425 my ($self, $method_name, $method) = @_;
427 push @{ $self->{before_method_modifiers}{$method_name} ||= [] }, $method;
430 sub add_around_method_modifier {
431 my ($self, $method_name, $method) = @_;
433 push @{ $self->{around_method_modifiers}{$method_name} ||= [] }, $method;
436 sub add_after_method_modifier {
437 my ($self, $method_name, $method) = @_;
439 push @{ $self->{after_method_modifiers}{$method_name} ||= [] }, $method;
443 sub get_before_method_modifiers {
444 my ($self, $method_name) = @_;
445 return @{ $self->{before_method_modifiers}{$method_name} ||= [] }
447 sub get_around_method_modifiers {
448 my ($self, $method_name) = @_;
449 return @{ $self->{around_method_modifiers}{$method_name} ||= [] }
451 sub get_after_method_modifiers {
452 my ($self, $method_name) = @_;
453 return @{ $self->{after_method_modifiers}{$method_name} ||= [] }
456 sub add_metaclass_accessor { # for meta roles (a.k.a. traits)
457 my($meta, $name) = @_;
458 $meta->add_method($name => $generate_class_accessor->($name));
462 package Mouse::Meta::Attribute;
464 require Mouse::Meta::Method::Accessor;
466 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
470 sub name { $_[0]->{name} }
471 sub associated_class { $_[0]->{associated_class} }
473 sub accessor { $_[0]->{accessor} }
474 sub reader { $_[0]->{reader} }
475 sub writer { $_[0]->{writer} }
476 sub predicate { $_[0]->{predicate} }
477 sub clearer { $_[0]->{clearer} }
478 sub handles { $_[0]->{handles} }
480 sub _is_metadata { $_[0]->{is} }
481 sub is_required { $_[0]->{required} }
482 sub default { $_[0]->{default} }
483 sub is_lazy { $_[0]->{lazy} }
484 sub is_lazy_build { $_[0]->{lazy_build} }
485 sub is_weak_ref { $_[0]->{weak_ref} }
486 sub init_arg { $_[0]->{init_arg} }
487 sub type_constraint { $_[0]->{type_constraint} }
489 sub trigger { $_[0]->{trigger} }
490 sub builder { $_[0]->{builder} }
491 sub should_auto_deref { $_[0]->{auto_deref} }
492 sub should_coerce { $_[0]->{coerce} }
494 sub documentation { $_[0]->{documentation} }
495 sub insertion_order { $_[0]->{insertion_order} }
499 sub has_accessor { exists $_[0]->{accessor} }
500 sub has_reader { exists $_[0]->{reader} }
501 sub has_writer { exists $_[0]->{writer} }
502 sub has_predicate { exists $_[0]->{predicate} }
503 sub has_clearer { exists $_[0]->{clearer} }
504 sub has_handles { exists $_[0]->{handles} }
506 sub has_default { exists $_[0]->{default} }
507 sub has_type_constraint { exists $_[0]->{type_constraint} }
508 sub has_trigger { exists $_[0]->{trigger} }
509 sub has_builder { exists $_[0]->{builder} }
511 sub has_documentation { exists $_[0]->{documentation} }
513 sub _process_options{
514 my($class, $name, $args) = @_;
516 # taken from Class::MOP::Attribute::new
519 or $class->throw_error('You must provide a name for the attribute');
521 if(!exists $args->{init_arg}){
522 $args->{init_arg} = $name;
525 # 'required' requires eigher 'init_arg', 'builder', or 'default'
526 my $can_be_required = defined( $args->{init_arg} );
528 if(exists $args->{builder}){
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};
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 [])");
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");
550 # taken from Mouse::Meta::Attribute->new and ->_process_args
552 if(exists $args->{is}){
553 my $is = $args->{is};
556 $args->{reader} ||= $name;
559 if(exists $args->{writer}){
560 $args->{reader} ||= $name;
563 $args->{accessor} ||= $name;
566 elsif($is eq 'bare'){
567 # do nothing, but don't complain (later) about missing methods
570 $is = 'undef' if !defined $is;
571 $class->throw_error("I do not understand this option (is => $is) on attribute ($name)");
576 if(exists $args->{isa}){
577 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($args->{isa});
580 if(exists $args->{does}){
581 if(defined $tc){ # both isa and does supplied
584 eval{ "$tc"->does($args) };
587 $class->throw_error("Cannot have both an isa option and a does option because '$tc' does not do '$args->{does}' on attribute ($name)");
591 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_does_type_constraint($args->{does});
597 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)");
600 && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)");
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)");
608 $args->{builder} ||= "_build_${name}";
610 $args->{clearer} ||= "_clear${name}";
611 $args->{predicate} ||= "_has${name}";
614 $args->{clearer} ||= "clear_${name}";
615 $args->{predicate} ||= "has_${name}";
619 if ($args->{auto_deref}) {
621 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)");
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)");
627 if (exists $args->{trigger}) {
628 ('CODE' eq ref $args->{trigger})
629 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)");
633 (exists $args->{default} || defined $args->{builder})
634 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it");
641 package Mouse::Meta::TypeConstraint;
644 '""' => '_as_string',
650 sub name { $_[0]->{name} }
651 sub parent { $_[0]->{parent} }
652 sub message { $_[0]->{message} }
654 sub type_parameter { $_[0]->{type_parameter} }
655 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
656 sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
658 sub __is_parameterized { exists $_[0]->{type_parameter} }
659 sub has_coercion { exists $_[0]->{_compiled_type_coercion} }
662 sub compile_type_constraint{
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
672 elsif($parent->{constraint}){
673 unshift @checks, $parent->{constraint};
678 if($self->{constraint}){
679 push @checks, $self->{constraint};
682 if($self->{type_constraints}){ # Union
683 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
685 foreach my $c(@types){
686 return 1 if $c->($_[0]);
693 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
696 $self->{compiled_type_constraint} = sub{
699 foreach my $c(@checks){
700 return undef if !$c->(@args);
710 return $self->_compiled_type_constraint->(@_);
714 package Mouse::Object;
719 if (scalar @_ == 1) {
720 (ref($_[0]) eq 'HASH')
721 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
733 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
735 my $args = $class->BUILDARGS(@_);
737 my $meta = Mouse::Meta::Class->initialize($class);
738 return $meta->new_object($args);
744 return unless $self->can('DEMOLISH'); # short circuit
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 ;)
758 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
759 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
762 $self->$demolish($Mouse::Util::in_global_destruction);
769 die $e if $e; # rethrow
776 return unless $self->can('BUILD');
778 for my $class (reverse $self->meta->linearized_isa) {
779 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
788 *DEMOLISHALL = \&DESTROY;
795 Mouse::PurePerl - A Mouse guts in pure Perl
799 This document describes Mouse version 0.70