1 package Mouse::PurePerl;
10 use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl
15 # taken from Class/MOP.pm
16 sub is_valid_class_name {
19 return 0 if ref($class);
20 return 0 unless defined($class);
22 return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms;
30 return 0 if ref($class) || !defined($class) || !length($class);
32 # walk the symbol table tree to avoid autovififying
33 # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::
36 foreach my $part (split('::', $class)) {
38 return 0 if !exists $pack->{$part};
40 my $entry = \$pack->{$part};
41 return 0 if ref($entry) ne 'GLOB';
42 $pack = *{$entry}{HASH};
45 return 0 if !%{$pack};
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;
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};
64 # taken from Sub::Identify
67 ref($coderef) or return;
69 my $cv = B::svref_2object($coderef);
70 $cv->isa('B::CV') or return;
73 $gv->isa('B::GV') or return;
75 return ($gv->STASH->NAME, $gv->NAME);
81 my $cv = B::svref_2object($coderef);
82 $cv->isa('B::CV') or return '';
85 $gv->isa('B::GV') or return '';
87 return $gv->STASH->NAME;
91 my($package, $name) = @_;
94 use warnings FATAL => 'uninitialized';
95 return *{$package . '::' . $name}{CODE};
98 sub generate_isa_predicate_for {
99 my($for_class, $name) = @_;
101 my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
104 Mouse::Util::install_subroutines(scalar caller, $name => $predicate);
111 sub generate_can_predicate_for {
112 my($methods_ref, $name) = @_;
114 my @methods = @{$methods_ref};
118 if(Scalar::Util::blessed($instance)){
119 foreach my $method(@methods){
120 if(!$instance->can($method)){
130 Mouse::Util::install_subroutines(scalar caller, $name => $predicate);
137 package Mouse::Util::TypeConstraints;
139 use Scalar::Util qw(blessed looks_like_number openhandle);
144 sub Bool { $_[0] ? $_[0] eq '1' : 1 }
145 sub Undef { !defined($_[0]) }
146 sub Defined { defined($_[0]) }
147 sub Value { defined($_[0]) && !ref($_[0]) }
148 sub Num { looks_like_number($_[0]) }
151 looks_like_number($value) && $value =~ /\A [+-]? [0-9]+ \z/xms;
155 return defined($value) && ref(\$value) eq 'SCALAR';
158 sub Ref { ref($_[0]) }
161 return ref($value) eq 'SCALAR'
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' }
170 return openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
173 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
175 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
176 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
178 sub _parameterize_ArrayRef_for {
179 my($type_parameter) = @_;
180 my $check = $type_parameter->_compiled_type_constraint;
183 foreach my $value (@{$_}) {
184 return undef unless $check->($value);
190 sub _parameterize_HashRef_for {
191 my($type_parameter) = @_;
192 my $check = $type_parameter->_compiled_type_constraint;
195 foreach my $value(values %{$_}){
196 return undef unless $check->($value);
202 # 'Maybe' type accepts 'Any', so it requires parameters
203 sub _parameterize_Maybe_for {
204 my($type_parameter) = @_;
205 my $check = $type_parameter->_compiled_type_constraint;
208 return !defined($_) || $check->($_);
212 package Mouse::Meta::Module;
214 sub name { $_[0]->{package} }
216 sub _method_map { $_[0]->{methods} }
217 sub _attribute_map{ $_[0]->{attributes} }
220 my $name = $_[0]->{package};
222 return \%{ $name . '::' };
226 my($self, $name, $code) = @_;
229 $self->throw_error('You must pass a defined name');
232 $self->throw_error('You must pass a defined code');
235 if(ref($code) ne 'CODE'){
236 $code = \&{$code}; # coerce
239 $self->{methods}->{$name} = $code; # Moose stores meta object here.
241 Mouse::Util::install_subroutines($self->name,
247 package Mouse::Meta::Class;
249 use Mouse::Meta::Method::Constructor;
250 use Mouse::Meta::Method::Destructor;
252 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
253 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
255 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
256 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
259 return exists $_[0]->{anon_serial_id};
262 sub roles { $_[0]->{roles} }
264 sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
266 sub get_all_attributes {
268 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
269 return values %attrs;
274 my %args = (@_ == 1 ? %{$_[0]} : @_);
276 my $object = bless {}, $meta->name;
278 $meta->_initialize_object($object, \%args);
280 if( $object->can('BUILD') ) {
281 for my $class (reverse $meta->linearized_isa) {
282 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
285 $object->$build(\%args);
294 my $args = $object->Mouse::Object::BUILDARGS(@_);
296 (blessed($object) && $object->isa($class->name))
297 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
299 my $cloned = bless { %$object }, ref $object;
300 $class->_initialize_object($cloned, $args, 1);
305 sub _initialize_object{
306 my($self, $object, $args, $is_cloning) = @_;
312 foreach my $attribute ($self->get_all_attributes) {
313 my $init_arg = $attribute->init_arg;
314 my $slot = $attribute->name;
316 if (defined($init_arg) && exists($args->{$init_arg})) {
317 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
319 weaken($object->{$slot})
320 if ref($object->{$slot}) && $attribute->is_weak_ref;
322 if ($attribute->has_trigger) {
323 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
328 if ($attribute->has_default || $attribute->has_builder) {
329 if (!$attribute->is_lazy && !exists $object->{$slot}) {
330 my $default = $attribute->default;
331 my $builder = $attribute->builder;
332 my $value = $builder ? $object->$builder()
333 : ref($default) eq 'CODE' ? $object->$default()
336 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
338 weaken($object->{$slot})
339 if ref($object->{$slot}) && $attribute->is_weak_ref;
342 elsif(!$is_cloning && $attribute->is_required) {
343 $self->throw_error("Attribute (".$attribute->name.") is required");
348 if($used < keys %{$args} && $self->strict_constructor) {
349 $self->_report_unknown_args([ $self->get_all_attributes ], $args);
353 foreach my $trigger_and_value(@triggers_queue){
354 my($trigger, $value) = @{$trigger_and_value};
355 $trigger->($object, $value);
359 if($self->is_anon_class){
360 $object->{__METACLASS__} = $self;
366 sub is_immutable { $_[0]->{is_immutable} }
368 sub strict_constructor{
371 $self->{strict_constructor} = shift;
374 foreach my $class($self->linearized_isa) {
375 my $meta = Mouse::Util::get_metaclass_by_name($class)
378 if(exists $meta->{strict_constructor}) {
379 return $meta->{strict_constructor};
386 sub _report_unknown_args {
387 my($metaclass, $attrs, $args) = @_;
391 foreach my $attr(@{$attrs}){
392 my $init_arg = $attr->init_arg;
393 if(defined $init_arg){
394 $init_args{$init_arg}++;
398 while(my $key = each %{$args}){
399 if(!exists $init_args{$key}){
400 push @unknowns, $key;
404 $metaclass->throw_error( sprintf
405 "Unknown attribute passed to the constructor of %s: %s",
406 $metaclass->name, Mouse::Util::english_list(@unknowns),
410 package Mouse::Meta::Role;
412 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
415 return exists $_[0]->{anon_serial_id};
418 sub get_roles { $_[0]->{roles} }
420 sub add_before_method_modifier {
421 my ($self, $method_name, $method) = @_;
423 push @{ $self->{before_method_modifiers}{$method_name} ||= [] }, $method;
426 sub add_around_method_modifier {
427 my ($self, $method_name, $method) = @_;
429 push @{ $self->{around_method_modifiers}{$method_name} ||= [] }, $method;
432 sub add_after_method_modifier {
433 my ($self, $method_name, $method) = @_;
435 push @{ $self->{after_method_modifiers}{$method_name} ||= [] }, $method;
439 sub get_before_method_modifiers {
440 my ($self, $method_name) = @_;
441 return @{ $self->{before_method_modifiers}{$method_name} ||= [] }
443 sub get_around_method_modifiers {
444 my ($self, $method_name) = @_;
445 return @{ $self->{around_method_modifiers}{$method_name} ||= [] }
447 sub get_after_method_modifiers {
448 my ($self, $method_name) = @_;
449 return @{ $self->{after_method_modifiers}{$method_name} ||= [] }
452 package Mouse::Meta::Attribute;
454 require Mouse::Meta::Method::Accessor;
456 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
460 sub name { $_[0]->{name} }
461 sub associated_class { $_[0]->{associated_class} }
463 sub accessor { $_[0]->{accessor} }
464 sub reader { $_[0]->{reader} }
465 sub writer { $_[0]->{writer} }
466 sub predicate { $_[0]->{predicate} }
467 sub clearer { $_[0]->{clearer} }
468 sub handles { $_[0]->{handles} }
470 sub _is_metadata { $_[0]->{is} }
471 sub is_required { $_[0]->{required} }
472 sub default { $_[0]->{default} }
473 sub is_lazy { $_[0]->{lazy} }
474 sub is_lazy_build { $_[0]->{lazy_build} }
475 sub is_weak_ref { $_[0]->{weak_ref} }
476 sub init_arg { $_[0]->{init_arg} }
477 sub type_constraint { $_[0]->{type_constraint} }
479 sub trigger { $_[0]->{trigger} }
480 sub builder { $_[0]->{builder} }
481 sub should_auto_deref { $_[0]->{auto_deref} }
482 sub should_coerce { $_[0]->{coerce} }
484 sub documentation { $_[0]->{documentation} }
485 sub insertion_order { $_[0]->{insertion_order} }
489 sub has_accessor { exists $_[0]->{accessor} }
490 sub has_reader { exists $_[0]->{reader} }
491 sub has_writer { exists $_[0]->{writer} }
492 sub has_predicate { exists $_[0]->{predicate} }
493 sub has_clearer { exists $_[0]->{clearer} }
494 sub has_handles { exists $_[0]->{handles} }
496 sub has_default { exists $_[0]->{default} }
497 sub has_type_constraint { exists $_[0]->{type_constraint} }
498 sub has_trigger { exists $_[0]->{trigger} }
499 sub has_builder { exists $_[0]->{builder} }
501 sub has_documentation { exists $_[0]->{documentation} }
503 sub _process_options{
504 my($class, $name, $args) = @_;
506 # taken from Class::MOP::Attribute::new
509 or $class->throw_error('You must provide a name for the attribute');
511 if(!exists $args->{init_arg}){
512 $args->{init_arg} = $name;
515 # 'required' requires eigher 'init_arg', 'builder', or 'default'
516 my $can_be_required = defined( $args->{init_arg} );
518 if(exists $args->{builder}){
520 # Moose refuses a CODE ref builder, but Mouse doesn't for backward compatibility
521 # This feature will be changed in a future. (gfx)
522 $class->throw_error('builder must be a defined scalar value which is a method name')
523 #if ref $args->{builder} || !defined $args->{builder};
524 if !defined $args->{builder};
528 elsif(exists $args->{default}){
529 if(ref $args->{default} && ref($args->{default}) ne 'CODE'){
530 $class->throw_error("References are not allowed as default values, you must "
531 . "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])");
536 if( $args->{required} && !$can_be_required ) {
537 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg");
540 # taken from Mouse::Meta::Attribute->new and ->_process_args
542 if(exists $args->{is}){
543 my $is = $args->{is};
546 $args->{reader} ||= $name;
549 if(exists $args->{writer}){
550 $args->{reader} ||= $name;
553 $args->{accessor} ||= $name;
556 elsif($is eq 'bare'){
557 # do nothing, but don't complain (later) about missing methods
560 $is = 'undef' if !defined $is;
561 $class->throw_error("I do not understand this option (is => $is) on attribute ($name)");
566 if(exists $args->{isa}){
567 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($args->{isa});
570 if(exists $args->{does}){
571 if(defined $tc){ # both isa and does supplied
574 eval{ "$tc"->does($args) };
577 $class->throw_error("Cannot have both an isa option and a does option because '$tc' does not do '$args->{does}' on attribute ($name)");
581 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_does_type_constraint($args->{does});
587 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)");
590 && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)");
593 if ($args->{lazy_build}) {
594 exists($args->{default})
595 && $class->throw_error("You can not use lazy_build and default for the same attribute ($name)");
598 $args->{builder} ||= "_build_${name}";
600 $args->{clearer} ||= "_clear${name}";
601 $args->{predicate} ||= "_has${name}";
604 $args->{clearer} ||= "clear_${name}";
605 $args->{predicate} ||= "has_${name}";
609 if ($args->{auto_deref}) {
611 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)");
613 ( $tc->is_a_type_of('ArrayRef') || $tc->is_a_type_of('HashRef') )
614 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)");
617 if (exists $args->{trigger}) {
618 ('CODE' eq ref $args->{trigger})
619 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)");
623 (exists $args->{default} || defined $args->{builder})
624 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it");
631 package Mouse::Meta::TypeConstraint;
633 sub name { $_[0]->{name} }
634 sub parent { $_[0]->{parent} }
635 sub message { $_[0]->{message} }
637 sub type_parameter { $_[0]->{type_parameter} }
638 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
639 sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
641 sub __is_parameterized { exists $_[0]->{type_parameter} }
642 sub has_coercion { exists $_[0]->{_compiled_type_coercion} }
645 sub compile_type_constraint{
650 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
651 if($parent->{hand_optimized_type_constraint}){
652 unshift @checks, $parent->{hand_optimized_type_constraint};
653 last; # a hand optimized constraint must include all the parents
655 elsif($parent->{constraint}){
656 unshift @checks, $parent->{constraint};
661 if($self->{constraint}){
662 push @checks, $self->{constraint};
665 if($self->{type_constraints}){ # Union
666 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
668 foreach my $c(@types){
669 return 1 if $c->($_[0]);
676 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
679 $self->{compiled_type_constraint} = sub{
682 foreach my $c(@checks){
683 return undef if !$c->(@args);
693 return $self->_compiled_type_constraint->(@_);
697 package Mouse::Object;
702 if (scalar @_ == 1) {
703 (ref($_[0]) eq 'HASH')
704 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
716 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
718 my $args = $class->BUILDARGS(@_);
720 my $meta = Mouse::Meta::Class->initialize($class);
721 return $meta->new_object($args);
727 return unless $self->can('DEMOLISH'); # short circuit
736 # We cannot count on being able to retrieve a previously made
737 # metaclass, _or_ being able to make a new one during global
738 # destruction. However, we should still be able to use mro at
739 # that time (at least tests suggest so ;)
741 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
742 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
745 $self->$demolish($Mouse::Util::in_global_destruction);
752 die $e if $e; # rethrow
759 return unless $self->can('BUILD');
761 for my $class (reverse $self->meta->linearized_isa) {
762 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
771 *DEMOLISHALL = \&DESTROY;
778 Mouse::PurePerl - A Mouse guts in pure Perl
782 This document describes Mouse version 0.64