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;
141 sub Bool { !$_[0] || $_[0] eq '1' }
142 sub Undef { !defined($_[0]) }
143 sub Defined { defined($_[0]) }
144 sub Value { defined($_[0]) && !ref($_[0]) }
145 sub Num { Scalar::Util::looks_like_number($_[0]) }
147 # We need to use a copy here to flatten MAGICs, for instance as in
148 # Str( substr($_, 0, 42) ).
150 return defined($value) && ref(\$value) eq 'SCALAR';
153 # We need to use a copy here to save the original internal SV flags.
155 return defined($value) && $value =~ /\A -? [0-9]+ \z/xms;
158 sub Ref { ref($_[0]) }
161 return ref($value) eq 'SCALAR' || ref($value) eq 'REF';
163 sub ArrayRef { ref($_[0]) eq 'ARRAY' }
164 sub HashRef { ref($_[0]) eq 'HASH' }
165 sub CodeRef { ref($_[0]) eq 'CODE' }
166 sub RegexpRef { ref($_[0]) eq 'Regexp' }
167 sub GlobRef { ref($_[0]) eq 'GLOB' }
171 return Scalar::Util::openhandle($value)
172 || (Scalar::Util::blessed($value) && $value->isa("IO::Handle"))
175 sub Object { Scalar::Util::blessed($_[0]) && ref($_[0]) ne 'Regexp' }
177 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
178 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
180 sub _parameterize_ArrayRef_for {
181 my($type_parameter) = @_;
182 my $check = $type_parameter->_compiled_type_constraint;
185 foreach my $value (@{$_}) {
186 return undef unless $check->($value);
192 sub _parameterize_HashRef_for {
193 my($type_parameter) = @_;
194 my $check = $type_parameter->_compiled_type_constraint;
197 foreach my $value(values %{$_}){
198 return undef unless $check->($value);
204 # 'Maybe' type accepts 'Any', so it requires parameters
205 sub _parameterize_Maybe_for {
206 my($type_parameter) = @_;
207 my $check = $type_parameter->_compiled_type_constraint;
210 return !defined($_) || $check->($_);
214 package Mouse::Meta::Module;
216 sub name { $_[0]->{package} }
218 sub _method_map { $_[0]->{methods} }
219 sub _attribute_map{ $_[0]->{attributes} }
222 my $name = $_[0]->{package};
224 return \%{ $name . '::' };
228 my($self, $name, $code) = @_;
231 $self->throw_error('You must pass a defined name');
234 $self->throw_error('You must pass a defined code');
237 if(ref($code) ne 'CODE'){
238 $code = \&{$code}; # coerce
241 $self->{methods}->{$name} = $code; # Moose stores meta object here.
243 Mouse::Util::install_subroutines($self->name,
249 my $generate_class_accessor = sub {
254 return $self->{$name} = shift;
257 foreach my $class($self->linearized_isa) {
258 my $meta = Mouse::Util::get_metaclass_by_name($class)
261 if(exists $meta->{$name}) {
262 return $meta->{$name};
270 package Mouse::Meta::Class;
272 use Mouse::Meta::Method::Constructor;
273 use Mouse::Meta::Method::Destructor;
275 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
276 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
278 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
279 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
282 return exists $_[0]->{anon_serial_id};
285 sub roles { $_[0]->{roles} }
287 sub linearized_isa { @{ Mouse::Util::get_linear_isa($_[0]->{package}) } }
291 my %args = (@_ == 1 ? %{$_[0]} : @_);
293 my $object = bless {}, $meta->name;
295 $meta->_initialize_object($object, \%args, 0);
297 if( $object->can('BUILD') ) {
298 for my $class (reverse $meta->linearized_isa) {
299 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
302 $object->$build(\%args);
311 my $args = $object->Mouse::Object::BUILDARGS(@_);
313 (Scalar::Util::blessed($object) && $object->isa($class->name))
314 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
316 my $cloned = bless { %$object }, ref $object;
317 $class->_initialize_object($cloned, $args, 1);
321 sub _initialize_object{
322 my($self, $object, $args, $is_cloning) = @_;
323 # The initializer, which is used everywhere, must be clear
324 # when an attribute is added. See Mouse::Meta::Class::add_attribute.
325 my $initializer = $self->{_mouse_cache}{_initialize_object} ||=
326 Mouse::Util::load_class($self->constructor_class)
327 ->_generate_initialize_object($self);
328 goto &{$initializer};
331 sub get_all_attributes {
333 return @{ $self->{_mouse_cache}{all_attributes}
334 ||= $self->_calculate_all_attributes };
337 sub is_immutable { $_[0]->{is_immutable} }
339 sub strict_constructor;
340 *strict_constructor = $generate_class_accessor->('strict_constructor');
342 sub _invalidate_metaclass_cache {
344 delete $self->{_mouse_cache};
348 sub _report_unknown_args {
349 my($metaclass, $attrs, $args) = @_;
353 foreach my $attr(@{$attrs}){
354 my $init_arg = $attr->init_arg;
355 if(defined $init_arg){
356 $init_args{$init_arg}++;
360 while(my $key = each %{$args}){
361 if(!exists $init_args{$key}){
362 push @unknowns, $key;
366 $metaclass->throw_error( sprintf
367 "Unknown attribute passed to the constructor of %s: %s",
368 $metaclass->name, Mouse::Util::english_list(@unknowns),
372 package Mouse::Meta::Role;
374 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
377 return exists $_[0]->{anon_serial_id};
380 sub get_roles { $_[0]->{roles} }
382 sub add_before_method_modifier {
383 my ($self, $method_name, $method) = @_;
385 push @{ $self->{before_method_modifiers}{$method_name} ||= [] }, $method;
388 sub add_around_method_modifier {
389 my ($self, $method_name, $method) = @_;
391 push @{ $self->{around_method_modifiers}{$method_name} ||= [] }, $method;
394 sub add_after_method_modifier {
395 my ($self, $method_name, $method) = @_;
397 push @{ $self->{after_method_modifiers}{$method_name} ||= [] }, $method;
401 sub get_before_method_modifiers {
402 my ($self, $method_name) = @_;
403 return @{ $self->{before_method_modifiers}{$method_name} ||= [] }
405 sub get_around_method_modifiers {
406 my ($self, $method_name) = @_;
407 return @{ $self->{around_method_modifiers}{$method_name} ||= [] }
409 sub get_after_method_modifiers {
410 my ($self, $method_name) = @_;
411 return @{ $self->{after_method_modifiers}{$method_name} ||= [] }
414 sub add_metaclass_accessor { # for meta roles (a.k.a. traits)
415 my($meta, $name) = @_;
416 $meta->add_method($name => $generate_class_accessor->($name));
420 package Mouse::Meta::Attribute;
422 require Mouse::Meta::Method::Accessor;
424 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
428 sub name { $_[0]->{name} }
429 sub associated_class { $_[0]->{associated_class} }
431 sub accessor { $_[0]->{accessor} }
432 sub reader { $_[0]->{reader} }
433 sub writer { $_[0]->{writer} }
434 sub predicate { $_[0]->{predicate} }
435 sub clearer { $_[0]->{clearer} }
436 sub handles { $_[0]->{handles} }
438 sub _is_metadata { $_[0]->{is} }
439 sub is_required { $_[0]->{required} }
440 sub default { $_[0]->{default} }
441 sub is_lazy { $_[0]->{lazy} }
442 sub is_lazy_build { $_[0]->{lazy_build} }
443 sub is_weak_ref { $_[0]->{weak_ref} }
444 sub init_arg { $_[0]->{init_arg} }
445 sub type_constraint { $_[0]->{type_constraint} }
447 sub trigger { $_[0]->{trigger} }
448 sub builder { $_[0]->{builder} }
449 sub should_auto_deref { $_[0]->{auto_deref} }
450 sub should_coerce { $_[0]->{coerce} }
452 sub documentation { $_[0]->{documentation} }
453 sub insertion_order { $_[0]->{insertion_order} }
457 sub has_accessor { exists $_[0]->{accessor} }
458 sub has_reader { exists $_[0]->{reader} }
459 sub has_writer { exists $_[0]->{writer} }
460 sub has_predicate { exists $_[0]->{predicate} }
461 sub has_clearer { exists $_[0]->{clearer} }
462 sub has_handles { exists $_[0]->{handles} }
464 sub has_default { exists $_[0]->{default} }
465 sub has_type_constraint { exists $_[0]->{type_constraint} }
466 sub has_trigger { exists $_[0]->{trigger} }
467 sub has_builder { exists $_[0]->{builder} }
469 sub has_documentation { exists $_[0]->{documentation} }
471 sub _process_options{
472 my($class, $name, $args) = @_;
474 # taken from Class::MOP::Attribute::new
477 or $class->throw_error('You must provide a name for the attribute');
479 if(!exists $args->{init_arg}){
480 $args->{init_arg} = $name;
483 # 'required' requires eigher 'init_arg', 'builder', or 'default'
484 my $can_be_required = defined( $args->{init_arg} );
486 if(exists $args->{builder}){
488 # Moose refuses a CODE ref builder, but Mouse doesn't for backward compatibility
489 # This feature will be changed in a future. (gfx)
490 $class->throw_error('builder must be a defined scalar value which is a method name')
491 #if ref $args->{builder} || !defined $args->{builder};
492 if !defined $args->{builder};
496 elsif(exists $args->{default}){
497 if(ref $args->{default} && ref($args->{default}) ne 'CODE'){
498 $class->throw_error("References are not allowed as default values, you must "
499 . "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])");
504 if( $args->{required} && !$can_be_required ) {
505 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg");
508 # taken from Mouse::Meta::Attribute->new and ->_process_args
510 if(exists $args->{is}){
511 my $is = $args->{is};
514 $args->{reader} ||= $name;
517 if(exists $args->{writer}){
518 $args->{reader} ||= $name;
521 $args->{accessor} ||= $name;
524 elsif($is eq 'bare'){
525 # do nothing, but don't complain (later) about missing methods
528 $is = 'undef' if !defined $is;
529 $class->throw_error("I do not understand this option (is => $is) on attribute ($name)");
534 if(exists $args->{isa}){
535 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($args->{isa});
538 if(exists $args->{does}){
539 if(defined $tc){ # both isa and does supplied
542 eval{ "$tc"->does($args->{does}) };
545 $class->throw_error("Cannot have both an isa option and a does option because '$tc' does not do '$args->{does}' on attribute ($name)");
549 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_does_type_constraint($args->{does});
555 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)");
558 && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)");
561 if ($args->{lazy_build}) {
562 exists($args->{default})
563 && $class->throw_error("You can not use lazy_build and default for the same attribute ($name)");
566 $args->{builder} ||= "_build_${name}";
568 $args->{clearer} ||= "_clear${name}";
569 $args->{predicate} ||= "_has${name}";
572 $args->{clearer} ||= "clear_${name}";
573 $args->{predicate} ||= "has_${name}";
577 if ($args->{auto_deref}) {
579 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)");
581 ( $tc->is_a_type_of('ArrayRef') || $tc->is_a_type_of('HashRef') )
582 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)");
585 if (exists $args->{trigger}) {
586 ('CODE' eq ref $args->{trigger})
587 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)");
591 (exists $args->{default} || defined $args->{builder})
592 || $class->throw_error("You cannot have a lazy attribute ($name) without specifying a default value for it");
599 package Mouse::Meta::TypeConstraint;
602 '""' => '_as_string',
608 sub name { $_[0]->{name} }
609 sub parent { $_[0]->{parent} }
610 sub message { $_[0]->{message} }
612 sub _identity { Scalar::Util::refaddr($_[0]) } # overload 0+
614 sub type_parameter { $_[0]->{type_parameter} }
615 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
617 sub __is_parameterized { exists $_[0]->{type_parameter} }
618 sub has_coercion { exists $_[0]->{_compiled_type_coercion} }
621 sub compile_type_constraint{
626 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
627 if($parent->{hand_optimized_type_constraint}){
628 unshift @checks, $parent->{hand_optimized_type_constraint};
629 last; # a hand optimized constraint must include all the parents
631 elsif($parent->{constraint}){
632 unshift @checks, $parent->{constraint};
637 if($self->{constraint}){
638 push @checks, $self->{constraint};
641 if($self->{type_constraints}){ # Union
642 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
644 foreach my $c(@types){
645 return 1 if $c->($_[0]);
652 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
655 $self->{compiled_type_constraint} = sub{
658 foreach my $c(@checks){
659 return undef if !$c->(@args);
669 return $self->_compiled_type_constraint->(@_);
673 package Mouse::Object;
678 if (scalar @_ == 1) {
679 (ref($_[0]) eq 'HASH')
680 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
691 my $args = $class->BUILDARGS(@_);
692 return $class->meta->new_object($args);
698 return unless $self->can('DEMOLISH'); # short circuit
706 # We cannot count on being able to retrieve a previously made
707 # metaclass, _or_ being able to make a new one during global
708 # destruction. However, we should still be able to use mro at
709 # that time (at least tests suggest so ;)
711 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
712 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
715 $self->$demolish($Mouse::Util::in_global_destruction);
722 die $e if $e; # rethrow
729 return unless $self->can('BUILD');
731 for my $class (reverse $self->meta->linearized_isa) {
732 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
741 *DEMOLISHALL = \&DESTROY;
748 Mouse::PurePerl - A Mouse guts in pure Perl
752 This document describes Mouse version 0.90