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;
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 { Scalar::Util::looks_like_number($_[0]) }
148 # We need to use a copy here to flatten MAGICs, for instance as in
149 # Str( substr($_, 0, 42) ).
151 return defined($value) && ref(\$value) eq 'SCALAR';
154 # We need to use a copy here to save the original internal SV flags.
156 return defined($value) && $value =~ /\A -? [0-9]+ \z/xms;
159 sub Ref { ref($_[0]) }
162 return ref($value) eq 'SCALAR' || ref($value) eq 'REF';
164 sub ArrayRef { ref($_[0]) eq 'ARRAY' }
165 sub HashRef { ref($_[0]) eq 'HASH' }
166 sub CodeRef { ref($_[0]) eq 'CODE' }
167 sub RegexpRef { ref($_[0]) eq 'Regexp' }
168 sub GlobRef { ref($_[0]) eq 'GLOB' }
172 return Scalar::Util::openhandle($value)
173 || (Scalar::Util::blessed($value) && $value->isa("IO::Handle"))
176 sub Object { Scalar::Util::blessed($_[0]) && ref($_[0]) ne 'Regexp' }
178 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
179 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
181 sub _parameterize_ArrayRef_for {
182 my($type_parameter) = @_;
183 my $check = $type_parameter->_compiled_type_constraint;
186 foreach my $value (@{$_}) {
187 return undef unless $check->($value);
193 sub _parameterize_HashRef_for {
194 my($type_parameter) = @_;
195 my $check = $type_parameter->_compiled_type_constraint;
198 foreach my $value(values %{$_}){
199 return undef unless $check->($value);
205 # 'Maybe' type accepts 'Any', so it requires parameters
206 sub _parameterize_Maybe_for {
207 my($type_parameter) = @_;
208 my $check = $type_parameter->_compiled_type_constraint;
211 return !defined($_) || $check->($_);
215 package Mouse::Meta::Module;
217 sub name { $_[0]->{package} }
219 sub _method_map { $_[0]->{methods} }
220 sub _attribute_map{ $_[0]->{attributes} }
223 my $name = $_[0]->{package};
225 return \%{ $name . '::' };
229 my($self, $name, $code) = @_;
232 $self->throw_error('You must pass a defined name');
235 $self->throw_error('You must pass a defined code');
238 if(ref($code) ne 'CODE'){
239 $code = \&{$code}; # coerce
242 $self->{methods}->{$name} = $code; # Moose stores meta object here.
244 Mouse::Util::install_subroutines($self->name,
250 my $generate_class_accessor = sub {
255 return $self->{$name} = shift;
258 foreach my $class($self->linearized_isa) {
259 my $meta = Mouse::Util::get_metaclass_by_name($class)
262 if(exists $meta->{$name}) {
263 return $meta->{$name};
271 package Mouse::Meta::Class;
273 use Mouse::Meta::Method::Constructor;
274 use Mouse::Meta::Method::Destructor;
276 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
277 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
279 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
280 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
283 return exists $_[0]->{anon_serial_id};
286 sub roles { $_[0]->{roles} }
288 sub linearized_isa { @{ Mouse::Util::get_linear_isa($_[0]->{package}) } }
292 my %args = (@_ == 1 ? %{$_[0]} : @_);
294 my $object = bless {}, $meta->name;
296 $meta->_initialize_object($object, \%args, 0);
298 if( $object->can('BUILD') ) {
299 for my $class (reverse $meta->linearized_isa) {
300 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
303 $object->$build(\%args);
312 my $args = $object->Mouse::Object::BUILDARGS(@_);
314 (Scalar::Util::blessed($object) && $object->isa($class->name))
315 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
317 my $cloned = bless { %$object }, ref $object;
318 $class->_initialize_object($cloned, $args, 1);
322 sub _initialize_object{
323 my($self, $object, $args, $is_cloning) = @_;
324 # The initializer, which is used everywhere, must be clear
325 # when an attribute is added. See Mouse::Meta::Class::add_attribute.
326 my $initializer = $self->{_mouse_cache}{_initialize_object} ||=
327 Mouse::Util::load_class($self->constructor_class)
328 ->_generate_initialize_object($self);
329 goto &{$initializer};
332 sub get_all_attributes {
334 return @{ $self->{_mouse_cache}{all_attributes}
335 ||= $self->_calculate_all_attributes };
338 sub is_immutable { $_[0]->{is_immutable} }
340 sub strict_constructor;
341 *strict_constructor = $generate_class_accessor->('strict_constructor');
343 sub _report_unknown_args {
344 my($metaclass, $attrs, $args) = @_;
348 foreach my $attr(@{$attrs}){
349 my $init_arg = $attr->init_arg;
350 if(defined $init_arg){
351 $init_args{$init_arg}++;
355 while(my $key = each %{$args}){
356 if(!exists $init_args{$key}){
357 push @unknowns, $key;
361 $metaclass->throw_error( sprintf
362 "Unknown attribute passed to the constructor of %s: %s",
363 $metaclass->name, Mouse::Util::english_list(@unknowns),
367 package Mouse::Meta::Role;
369 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
372 return exists $_[0]->{anon_serial_id};
375 sub get_roles { $_[0]->{roles} }
377 sub add_before_method_modifier {
378 my ($self, $method_name, $method) = @_;
380 push @{ $self->{before_method_modifiers}{$method_name} ||= [] }, $method;
383 sub add_around_method_modifier {
384 my ($self, $method_name, $method) = @_;
386 push @{ $self->{around_method_modifiers}{$method_name} ||= [] }, $method;
389 sub add_after_method_modifier {
390 my ($self, $method_name, $method) = @_;
392 push @{ $self->{after_method_modifiers}{$method_name} ||= [] }, $method;
396 sub get_before_method_modifiers {
397 my ($self, $method_name) = @_;
398 return @{ $self->{before_method_modifiers}{$method_name} ||= [] }
400 sub get_around_method_modifiers {
401 my ($self, $method_name) = @_;
402 return @{ $self->{around_method_modifiers}{$method_name} ||= [] }
404 sub get_after_method_modifiers {
405 my ($self, $method_name) = @_;
406 return @{ $self->{after_method_modifiers}{$method_name} ||= [] }
409 sub add_metaclass_accessor { # for meta roles (a.k.a. traits)
410 my($meta, $name) = @_;
411 $meta->add_method($name => $generate_class_accessor->($name));
415 package Mouse::Meta::Attribute;
417 require Mouse::Meta::Method::Accessor;
419 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
423 sub name { $_[0]->{name} }
424 sub associated_class { $_[0]->{associated_class} }
426 sub accessor { $_[0]->{accessor} }
427 sub reader { $_[0]->{reader} }
428 sub writer { $_[0]->{writer} }
429 sub predicate { $_[0]->{predicate} }
430 sub clearer { $_[0]->{clearer} }
431 sub handles { $_[0]->{handles} }
433 sub _is_metadata { $_[0]->{is} }
434 sub is_required { $_[0]->{required} }
435 sub default { $_[0]->{default} }
436 sub is_lazy { $_[0]->{lazy} }
437 sub is_lazy_build { $_[0]->{lazy_build} }
438 sub is_weak_ref { $_[0]->{weak_ref} }
439 sub init_arg { $_[0]->{init_arg} }
440 sub type_constraint { $_[0]->{type_constraint} }
442 sub trigger { $_[0]->{trigger} }
443 sub builder { $_[0]->{builder} }
444 sub should_auto_deref { $_[0]->{auto_deref} }
445 sub should_coerce { $_[0]->{coerce} }
447 sub documentation { $_[0]->{documentation} }
448 sub insertion_order { $_[0]->{insertion_order} }
452 sub has_accessor { exists $_[0]->{accessor} }
453 sub has_reader { exists $_[0]->{reader} }
454 sub has_writer { exists $_[0]->{writer} }
455 sub has_predicate { exists $_[0]->{predicate} }
456 sub has_clearer { exists $_[0]->{clearer} }
457 sub has_handles { exists $_[0]->{handles} }
459 sub has_default { exists $_[0]->{default} }
460 sub has_type_constraint { exists $_[0]->{type_constraint} }
461 sub has_trigger { exists $_[0]->{trigger} }
462 sub has_builder { exists $_[0]->{builder} }
464 sub has_documentation { exists $_[0]->{documentation} }
466 sub _process_options{
467 my($class, $name, $args) = @_;
469 # taken from Class::MOP::Attribute::new
472 or $class->throw_error('You must provide a name for the attribute');
474 if(!exists $args->{init_arg}){
475 $args->{init_arg} = $name;
478 # 'required' requires eigher 'init_arg', 'builder', or 'default'
479 my $can_be_required = defined( $args->{init_arg} );
481 if(exists $args->{builder}){
483 # Moose refuses a CODE ref builder, but Mouse doesn't for backward compatibility
484 # This feature will be changed in a future. (gfx)
485 $class->throw_error('builder must be a defined scalar value which is a method name')
486 #if ref $args->{builder} || !defined $args->{builder};
487 if !defined $args->{builder};
491 elsif(exists $args->{default}){
492 if(ref $args->{default} && ref($args->{default}) ne 'CODE'){
493 $class->throw_error("References are not allowed as default values, you must "
494 . "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])");
499 if( $args->{required} && !$can_be_required ) {
500 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg");
503 # taken from Mouse::Meta::Attribute->new and ->_process_args
505 if(exists $args->{is}){
506 my $is = $args->{is};
509 $args->{reader} ||= $name;
512 if(exists $args->{writer}){
513 $args->{reader} ||= $name;
516 $args->{accessor} ||= $name;
519 elsif($is eq 'bare'){
520 # do nothing, but don't complain (later) about missing methods
523 $is = 'undef' if !defined $is;
524 $class->throw_error("I do not understand this option (is => $is) on attribute ($name)");
529 if(exists $args->{isa}){
530 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($args->{isa});
533 if(exists $args->{does}){
534 if(defined $tc){ # both isa and does supplied
537 eval{ "$tc"->does($args) };
540 $class->throw_error("Cannot have both an isa option and a does option because '$tc' does not do '$args->{does}' on attribute ($name)");
544 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_does_type_constraint($args->{does});
550 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)");
553 && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)");
556 if ($args->{lazy_build}) {
557 exists($args->{default})
558 && $class->throw_error("You can not use lazy_build and default for the same attribute ($name)");
561 $args->{builder} ||= "_build_${name}";
563 $args->{clearer} ||= "_clear${name}";
564 $args->{predicate} ||= "_has${name}";
567 $args->{clearer} ||= "clear_${name}";
568 $args->{predicate} ||= "has_${name}";
572 if ($args->{auto_deref}) {
574 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)");
576 ( $tc->is_a_type_of('ArrayRef') || $tc->is_a_type_of('HashRef') )
577 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)");
580 if (exists $args->{trigger}) {
581 ('CODE' eq ref $args->{trigger})
582 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)");
586 (exists $args->{default} || defined $args->{builder})
587 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it");
594 package Mouse::Meta::TypeConstraint;
597 '""' => '_as_string',
603 sub name { $_[0]->{name} }
604 sub parent { $_[0]->{parent} }
605 sub message { $_[0]->{message} }
607 sub _identity { Scalar::Util::refaddr($_[0]) } # overload 0+
609 sub type_parameter { $_[0]->{type_parameter} }
610 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
611 sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
613 sub __is_parameterized { exists $_[0]->{type_parameter} }
614 sub has_coercion { exists $_[0]->{_compiled_type_coercion} }
617 sub compile_type_constraint{
622 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
623 if($parent->{hand_optimized_type_constraint}){
624 unshift @checks, $parent->{hand_optimized_type_constraint};
625 last; # a hand optimized constraint must include all the parents
627 elsif($parent->{constraint}){
628 unshift @checks, $parent->{constraint};
633 if($self->{constraint}){
634 push @checks, $self->{constraint};
637 if($self->{type_constraints}){ # Union
638 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
640 foreach my $c(@types){
641 return 1 if $c->($_[0]);
648 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
651 $self->{compiled_type_constraint} = sub{
654 foreach my $c(@checks){
655 return undef if !$c->(@args);
665 return $self->_compiled_type_constraint->(@_);
669 package Mouse::Object;
674 if (scalar @_ == 1) {
675 (ref($_[0]) eq 'HASH')
676 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
687 my $args = $class->BUILDARGS(@_);
688 return $class->meta->new_object($args);
694 return unless $self->can('DEMOLISH'); # short circuit
702 # We cannot count on being able to retrieve a previously made
703 # metaclass, _or_ being able to make a new one during global
704 # destruction. However, we should still be able to use mro at
705 # that time (at least tests suggest so ;)
707 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
708 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
711 $self->$demolish($Mouse::Util::in_global_destruction);
718 die $e if $e; # rethrow
725 return unless $self->can('BUILD');
727 for my $class (reverse $self->meta->linearized_isa) {
728 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
737 *DEMOLISHALL = \&DESTROY;
744 Mouse::PurePerl - A Mouse guts in pure Perl
748 This document describes Mouse version 0.75