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) };
105 *{ caller() . '::' . $name } = $predicate;
112 sub generate_can_predicate_for {
113 my($methods_ref, $name) = @_;
115 my @methods = @{$methods_ref};
119 if(Scalar::Util::blessed($instance)){
120 foreach my $method(@methods){
121 if(!$instance->can($method)){
132 *{ caller() . '::' . $name } = $predicate;
139 package Mouse::Util::TypeConstraints;
141 use Scalar::Util qw(blessed looks_like_number openhandle);
146 sub Bool { $_[0] ? $_[0] eq '1' : 1 }
147 sub Undef { !defined($_[0]) }
148 sub Defined { defined($_[0]) }
149 sub Value { defined($_[0]) && !ref($_[0]) }
150 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
151 sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
154 return defined($value) && ref(\$value) eq 'SCALAR';
157 sub Ref { ref($_[0]) }
160 return ref($value) eq 'SCALAR'
162 sub ArrayRef { ref($_[0]) eq 'ARRAY' }
163 sub HashRef { ref($_[0]) eq 'HASH' }
164 sub CodeRef { ref($_[0]) eq 'CODE' }
165 sub RegexpRef { ref($_[0]) eq 'Regexp' }
166 sub GlobRef { ref($_[0]) eq 'GLOB' }
169 return openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
172 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
174 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
175 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
177 sub _parameterize_ArrayRef_for {
178 my($type_parameter) = @_;
179 my $check = $type_parameter->_compiled_type_constraint;
182 foreach my $value (@{$_}) {
183 return undef unless $check->($value);
189 sub _parameterize_HashRef_for {
190 my($type_parameter) = @_;
191 my $check = $type_parameter->_compiled_type_constraint;
194 foreach my $value(values %{$_}){
195 return undef unless $check->($value);
201 # 'Maybe' type accepts 'Any', so it requires parameters
202 sub _parameterize_Maybe_for {
203 my($type_parameter) = @_;
204 my $check = $type_parameter->_compiled_type_constraint;
207 return !defined($_) || $check->($_);
211 package Mouse::Meta::Module;
213 sub name { $_[0]->{package} }
215 sub _method_map { $_[0]->{methods} }
216 sub _attribute_map{ $_[0]->{attributes} }
219 my $name = $_[0]->{package};
221 return \%{ $name . '::' };
225 my($self, $name, $code) = @_;
228 $self->throw_error('You must pass a defined name');
231 $self->throw_error('You must pass a defined code');
234 if(ref($code) ne 'CODE'){
235 $code = \&{$code}; # coerce
238 $self->{methods}->{$name} = $code; # Moose stores meta object here.
240 my $pkg = $self->name;
242 no warnings 'redefine', 'once';
243 *{ $pkg . '::' . $name } = $code;
247 package Mouse::Meta::Class;
249 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
250 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
252 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
253 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
256 return exists $_[0]->{anon_serial_id};
259 sub roles { $_[0]->{roles} }
261 sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
263 sub get_all_attributes {
265 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
266 return values %attrs;
271 my %args = (@_ == 1 ? %{$_[0]} : @_);
273 my $object = bless {}, $self->name;
275 $self->_initialize_object($object, \%args);
279 sub _initialize_object{
280 my($self, $object, $args, $ignore_triggers) = @_;
284 foreach my $attribute ($self->get_all_attributes) {
285 my $init_arg = $attribute->init_arg;
286 my $slot = $attribute->name;
288 if (defined($init_arg) && exists($args->{$init_arg})) {
289 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
291 weaken($object->{$slot})
292 if ref($object->{$slot}) && $attribute->is_weak_ref;
294 if ($attribute->has_trigger) {
295 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
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()
307 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
309 weaken($object->{$slot})
310 if ref($object->{$slot}) && $attribute->is_weak_ref;
313 elsif($attribute->is_required) {
314 $self->throw_error("Attribute (".$attribute->name.") is required");
319 if(!$ignore_triggers){
320 foreach my $trigger_and_value(@triggers_queue){
321 my($trigger, $value) = @{$trigger_and_value};
322 $trigger->($object, $value);
326 if($self->is_anon_class){
327 $object->{__METACLASS__} = $self;
333 sub is_immutable { $_[0]->{is_immutable} }
335 sub __strict_constructor{ $_[0]->{strict_constructor} }
337 package Mouse::Meta::Role;
339 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
342 return exists $_[0]->{anon_serial_id};
345 sub get_roles { $_[0]->{roles} }
347 package Mouse::Meta::Attribute;
349 require Mouse::Meta::Method::Accessor;
351 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
355 sub name { $_[0]->{name} }
356 sub associated_class { $_[0]->{associated_class} }
358 sub accessor { $_[0]->{accessor} }
359 sub reader { $_[0]->{reader} }
360 sub writer { $_[0]->{writer} }
361 sub predicate { $_[0]->{predicate} }
362 sub clearer { $_[0]->{clearer} }
363 sub handles { $_[0]->{handles} }
365 sub _is_metadata { $_[0]->{is} }
366 sub is_required { $_[0]->{required} }
367 sub default { $_[0]->{default} }
368 sub is_lazy { $_[0]->{lazy} }
369 sub is_lazy_build { $_[0]->{lazy_build} }
370 sub is_weak_ref { $_[0]->{weak_ref} }
371 sub init_arg { $_[0]->{init_arg} }
372 sub type_constraint { $_[0]->{type_constraint} }
374 sub trigger { $_[0]->{trigger} }
375 sub builder { $_[0]->{builder} }
376 sub should_auto_deref { $_[0]->{auto_deref} }
377 sub should_coerce { $_[0]->{coerce} }
379 sub documentation { $_[0]->{documentation} }
383 sub has_accessor { exists $_[0]->{accessor} }
384 sub has_reader { exists $_[0]->{reader} }
385 sub has_writer { exists $_[0]->{writer} }
386 sub has_predicate { exists $_[0]->{predicate} }
387 sub has_clearer { exists $_[0]->{clearer} }
388 sub has_handles { exists $_[0]->{handles} }
390 sub has_default { exists $_[0]->{default} }
391 sub has_type_constraint { exists $_[0]->{type_constraint} }
392 sub has_trigger { exists $_[0]->{trigger} }
393 sub has_builder { exists $_[0]->{builder} }
395 sub has_documentation { exists $_[0]->{documentation} }
397 sub _process_options{
398 my($class, $name, $args) = @_;
400 # taken from Class::MOP::Attribute::new
403 or $class->throw_error('You must provide a name for the attribute');
405 if(!exists $args->{init_arg}){
406 $args->{init_arg} = $name;
409 # 'required' requires eigher 'init_arg', 'builder', or 'default'
410 my $can_be_required = defined( $args->{init_arg} );
412 if(exists $args->{builder}){
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};
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 [])");
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");
434 # taken from Mouse::Meta::Attribute->new and ->_process_args
436 if(exists $args->{is}){
437 my $is = $args->{is};
440 $args->{reader} ||= $name;
443 if(exists $args->{writer}){
444 $args->{reader} ||= $name;
447 $args->{accessor} ||= $name;
450 elsif($is eq 'bare'){
451 # do nothing, but don't complain (later) about missing methods
454 $is = 'undef' if !defined $is;
455 $class->throw_error("I do not understand this option (is => $is) on attribute ($name)");
460 if(exists $args->{isa}){
461 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($args->{isa});
464 if(exists $args->{does}){
465 if(defined $tc){ # both isa and does supplied
468 eval{ "$tc"->does($args) };
471 $class->throw_error("Cannot have both an isa option and a does option because '$tc' does not do '$args->{does}' on attribute ($name)");
475 $tc = $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_does_type_constraint($args->{does});
481 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)");
484 && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)");
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)");
492 $args->{builder} ||= "_build_${name}";
494 $args->{clearer} ||= "_clear${name}";
495 $args->{predicate} ||= "_has${name}";
498 $args->{clearer} ||= "clear_${name}";
499 $args->{predicate} ||= "has_${name}";
503 if ($args->{auto_deref}) {
505 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)");
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)");
511 if (exists $args->{trigger}) {
512 ('CODE' eq ref $args->{trigger})
513 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)");
517 (exists $args->{default} || defined $args->{builder})
518 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it");
525 package Mouse::Meta::TypeConstraint;
527 sub name { $_[0]->{name} }
528 sub parent { $_[0]->{parent} }
529 sub message { $_[0]->{message} }
531 sub type_parameter { $_[0]->{type_parameter} }
532 sub __is_parameterized { exists $_[0]->{type_parameter} }
534 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
536 sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
538 sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
541 sub compile_type_constraint{
546 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
547 if($parent->{hand_optimized_type_constraint}){
548 unshift @checks, $parent->{hand_optimized_type_constraint};
549 last; # a hand optimized constraint must include all the parents
551 elsif($parent->{constraint}){
552 unshift @checks, $parent->{constraint};
557 if($self->{constraint}){
558 push @checks, $self->{constraint};
561 if($self->{type_constraints}){ # Union
562 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
564 foreach my $c(@types){
565 return 1 if $c->($_[0]);
572 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
575 $self->{compiled_type_constraint} = sub{
578 foreach my $c(@checks){
579 return undef if !$c->(@args);
587 package Mouse::Object;
593 if (scalar @_ == 1) {
594 (ref($_[0]) eq 'HASH')
595 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
607 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
609 my $args = $class->BUILDARGS(@_);
611 my $meta = Mouse::Meta::Class->initialize($class);
612 my $self = $meta->new_object($args);
615 if( $self->can('BUILD') ) {
616 for my $class (reverse $meta->linearized_isa) {
617 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
620 $self->$build($args);
630 return unless $self->can('DEMOLISH'); # short circuit
640 # We cannot count on being able to retrieve a previously made
641 # metaclass, _or_ being able to make a new one during global
642 # destruction. However, we should still be able to use mro at
643 # that time (at least tests suggest so ;)
645 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
646 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
649 $self->$demolish($Mouse::Util::in_global_destruction);
656 die $e if $e; # rethrow
663 return unless $self->can('BUILD');
665 for my $class (reverse $self->meta->linearized_isa) {
666 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
675 *DEMOLISHALL = \&DESTROY;
682 Mouse::PurePerl - A Mouse guts in pure Perl
686 This document describes Mouse version 0.50_03