1 package Mouse::PurePerl;
10 use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl
17 return 0 if ref($class) || !defined($class) || !length($class);
19 # walk the symbol table tree to avoid autovififying
20 # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::
23 foreach my $part (split('::', $class)) {
25 return 0 if !exists $pack->{$part};
27 my $entry = \$pack->{$part};
28 return 0 if ref($entry) ne 'GLOB';
29 $pack = *{$entry}{HASH};
32 return 0 if !%{$pack};
34 # check for $VERSION or @ISA
35 return 1 if exists $pack->{VERSION}
36 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
37 return 1 if exists $pack->{ISA}
38 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
40 # check for any method
41 foreach my $name( keys %{$pack} ) {
42 my $entry = \$pack->{$name};
43 return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
51 # taken from Sub::Identify
54 ref($coderef) or return;
56 my $cv = B::svref_2object($coderef);
57 $cv->isa('B::CV') or return;
60 $gv->isa('B::GV') or return;
62 return ($gv->STASH->NAME, $gv->NAME);
68 my $cv = B::svref_2object($coderef);
69 $cv->isa('B::CV') or return '';
72 $gv->isa('B::GV') or return '';
74 return $gv->STASH->NAME;
78 my($package, $name) = @_;
81 use warnings FATAL => 'uninitialized';
82 return *{$package . '::' . $name}{CODE};
85 sub generate_isa_predicate_for {
86 my($for_class, $name) = @_;
88 my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
92 *{ caller() . '::' . $name } = $predicate;
99 sub generate_can_predicate_for {
100 my($methods_ref, $name) = @_;
102 my @methods = @{$methods_ref};
106 if(Scalar::Util::blessed($instance)){
107 foreach my $method(@methods){
108 if(!$instance->can($method)){
119 *{ caller() . '::' . $name } = $predicate;
126 package Mouse::Util::TypeConstraints;
128 use Scalar::Util qw(blessed looks_like_number openhandle);
133 sub Bool { $_[0] ? $_[0] eq '1' : 1 }
134 sub Undef { !defined($_[0]) }
135 sub Defined { defined($_[0]) }
136 sub Value { defined($_[0]) && !ref($_[0]) }
137 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
138 sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
141 return defined($value) && ref(\$value) eq 'SCALAR';
144 sub Ref { ref($_[0]) }
147 return ref($value) eq 'SCALAR'
149 sub ArrayRef { ref($_[0]) eq 'ARRAY' }
150 sub HashRef { ref($_[0]) eq 'HASH' }
151 sub CodeRef { ref($_[0]) eq 'CODE' }
152 sub RegexpRef { ref($_[0]) eq 'Regexp' }
153 sub GlobRef { ref($_[0]) eq 'GLOB' }
156 return openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
159 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
161 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
162 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
164 sub _parameterize_ArrayRef_for {
165 my($type_parameter) = @_;
166 my $check = $type_parameter->_compiled_type_constraint;
169 foreach my $value (@{$_}) {
170 return undef unless $check->($value);
176 sub _parameterize_HashRef_for {
177 my($type_parameter) = @_;
178 my $check = $type_parameter->_compiled_type_constraint;
181 foreach my $value(values %{$_}){
182 return undef unless $check->($value);
188 # 'Maybe' type accepts 'Any', so it requires parameters
189 sub _parameterize_Maybe_for {
190 my($type_parameter) = @_;
191 my $check = $type_parameter->_compiled_type_constraint;
194 return !defined($_) || $check->($_);
200 package Mouse::Meta::Module;
202 sub name { $_[0]->{package} }
204 sub _method_map { $_[0]->{methods} }
205 sub _attribute_map{ $_[0]->{attributes} }
208 my $name = $_[0]->{package};
210 return \%{ $name . '::' };
214 my($self, $name, $code) = @_;
217 $self->throw_error('You must pass a defined name');
220 $self->throw_error('You must pass a defined code');
223 if(ref($code) ne 'CODE'){
224 $code = \&{$code}; # coerce
227 $self->{methods}->{$name} = $code; # Moose stores meta object here.
229 my $pkg = $self->name;
231 no warnings 'redefine', 'once';
232 *{ $pkg . '::' . $name } = $code;
236 package Mouse::Meta::Class;
238 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
239 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
241 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
242 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
245 return exists $_[0]->{anon_serial_id};
248 sub roles { $_[0]->{roles} }
250 sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
252 sub get_all_attributes {
254 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
255 return values %attrs;
260 my %args = (@_ == 1 ? %{$_[0]} : @_);
262 my $object = bless {}, $self->name;
264 $self->_initialize_object($object, \%args);
268 sub _initialize_object{
269 my($self, $object, $args, $ignore_triggers) = @_;
273 foreach my $attribute ($self->get_all_attributes) {
274 my $init_arg = $attribute->init_arg;
275 my $slot = $attribute->name;
277 if (defined($init_arg) && exists($args->{$init_arg})) {
278 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
280 weaken($object->{$slot})
281 if ref($object->{$slot}) && $attribute->is_weak_ref;
283 if ($attribute->has_trigger) {
284 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
288 if ($attribute->has_default || $attribute->has_builder) {
289 if (!$attribute->is_lazy) {
290 my $default = $attribute->default;
291 my $builder = $attribute->builder;
292 my $value = $builder ? $object->$builder()
293 : ref($default) eq 'CODE' ? $object->$default()
296 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
298 weaken($object->{$slot})
299 if ref($object->{$slot}) && $attribute->is_weak_ref;
302 elsif($attribute->is_required) {
303 $self->throw_error("Attribute (".$attribute->name.") is required");
308 if(!$ignore_triggers){
309 foreach my $trigger_and_value(@triggers_queue){
310 my($trigger, $value) = @{$trigger_and_value};
311 $trigger->($object, $value);
315 if($self->is_anon_class){
316 $object->{__METACLASS__} = $self;
322 sub is_immutable { $_[0]->{is_immutable} }
324 sub __strict_constructor{ $_[0]->{strict_constructor} }
326 package Mouse::Meta::Role;
328 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
331 return exists $_[0]->{anon_serial_id};
334 sub get_roles { $_[0]->{roles} }
336 package Mouse::Meta::Attribute;
338 require Mouse::Meta::Method::Accessor;
340 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
344 sub name { $_[0]->{name} }
345 sub associated_class { $_[0]->{associated_class} }
347 sub accessor { $_[0]->{accessor} }
348 sub reader { $_[0]->{reader} }
349 sub writer { $_[0]->{writer} }
350 sub predicate { $_[0]->{predicate} }
351 sub clearer { $_[0]->{clearer} }
352 sub handles { $_[0]->{handles} }
354 sub _is_metadata { $_[0]->{is} }
355 sub is_required { $_[0]->{required} }
356 sub default { $_[0]->{default} }
357 sub is_lazy { $_[0]->{lazy} }
358 sub is_lazy_build { $_[0]->{lazy_build} }
359 sub is_weak_ref { $_[0]->{weak_ref} }
360 sub init_arg { $_[0]->{init_arg} }
361 sub type_constraint { $_[0]->{type_constraint} }
363 sub trigger { $_[0]->{trigger} }
364 sub builder { $_[0]->{builder} }
365 sub should_auto_deref { $_[0]->{auto_deref} }
366 sub should_coerce { $_[0]->{coerce} }
368 sub documentation { $_[0]->{documentation} }
372 sub has_accessor { exists $_[0]->{accessor} }
373 sub has_reader { exists $_[0]->{reader} }
374 sub has_writer { exists $_[0]->{writer} }
375 sub has_predicate { exists $_[0]->{predicate} }
376 sub has_clearer { exists $_[0]->{clearer} }
377 sub has_handles { exists $_[0]->{handles} }
379 sub has_default { exists $_[0]->{default} }
380 sub has_type_constraint { exists $_[0]->{type_constraint} }
381 sub has_trigger { exists $_[0]->{trigger} }
382 sub has_builder { exists $_[0]->{builder} }
384 sub has_documentation { exists $_[0]->{documentation} }
386 sub _process_options{
387 my($class, $name, $args) = @_;
389 # taken from Class::MOP::Attribute::new
392 or $class->throw_error('You must provide a name for the attribute');
394 if(!exists $args->{init_arg}){
395 $args->{init_arg} = $name;
398 # 'required' requires eigher 'init_arg', 'builder', or 'default'
399 my $can_be_required = defined( $args->{init_arg} );
401 if(exists $args->{builder}){
403 # Moose refuses a CODE ref builder, but Mouse doesn't for backward compatibility
404 # This feature will be changed in a future. (gfx)
405 $class->throw_error('builder must be a defined scalar value which is a method name')
406 #if ref $args->{builder} || !defined $args->{builder};
407 if !defined $args->{builder};
411 elsif(exists $args->{default}){
412 if(ref $args->{default} && ref($args->{default}) ne 'CODE'){
413 $class->throw_error("References are not allowed as default values, you must "
414 . "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])");
419 if( $args->{required} && !$can_be_required ) {
420 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg");
423 # taken from Mouse::Meta::Attribute->new and ->_process_args
425 if(exists $args->{is}){
426 my $is = $args->{is};
429 $args->{reader} ||= $name;
432 if(exists $args->{writer}){
433 $args->{reader} ||= $name;
436 $args->{accessor} ||= $name;
439 elsif($is eq 'bare'){
440 # do nothing, but don't complain (later) about missing methods
443 $is = 'undef' if !defined $is;
444 $class->throw_error("I do not understand this option (is => $is) on attribute ($name)");
449 if(exists $args->{isa}){
450 $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($args->{isa});
452 elsif(exists $args->{does}){
453 $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_does_type_constraint($args->{does});
455 $tc = $args->{type_constraint};
459 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)");
462 && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)");
465 if ($args->{lazy_build}) {
466 exists($args->{default})
467 && $class->throw_error("You can not use lazy_build and default for the same attribute ($name)");
470 $args->{builder} ||= "_build_${name}";
472 $args->{clearer} ||= "_clear${name}";
473 $args->{predicate} ||= "_has${name}";
476 $args->{clearer} ||= "clear_${name}";
477 $args->{predicate} ||= "has_${name}";
481 if ($args->{auto_deref}) {
483 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)");
485 ( $tc->is_a_type_of('ArrayRef') || $tc->is_a_type_of('HashRef') )
486 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)");
489 if (exists $args->{trigger}) {
490 ('CODE' eq ref $args->{trigger})
491 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)");
495 (exists $args->{default} || defined $args->{builder})
496 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it");
503 package Mouse::Meta::TypeConstraint;
505 sub name { $_[0]->{name} }
506 sub parent { $_[0]->{parent} }
507 sub message { $_[0]->{message} }
509 sub type_parameter { $_[0]->{type_parameter} }
510 sub __is_parameterized { exists $_[0]->{type_parameter} }
512 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
514 sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
516 sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
519 sub compile_type_constraint{
524 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
525 if($parent->{hand_optimized_type_constraint}){
526 unshift @checks, $parent->{hand_optimized_type_constraint};
527 last; # a hand optimized constraint must include all the parents
529 elsif($parent->{constraint}){
530 unshift @checks, $parent->{constraint};
535 if($self->{constraint}){
536 push @checks, $self->{constraint};
539 if($self->{type_constraints}){ # Union
540 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
542 foreach my $c(@types){
543 return 1 if $c->($_[0]);
550 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
553 $self->{compiled_type_constraint} = sub{
556 foreach my $c(@checks){
557 return undef if !$c->(@args);
565 package Mouse::Object;
571 if (scalar @_ == 1) {
572 (ref($_[0]) eq 'HASH')
573 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
585 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
587 my $args = $class->BUILDARGS(@_);
589 my $meta = Mouse::Meta::Class->initialize($class);
590 my $self = $meta->new_object($args);
593 if( $self->can('BUILD') ) {
594 for my $class (reverse $meta->linearized_isa) {
595 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
598 $self->$build($args);
608 return unless $self->can('DEMOLISH'); # short circuit
618 # We cannot count on being able to retrieve a previously made
619 # metaclass, _or_ being able to make a new one during global
620 # destruction. However, we should still be able to use mro at
621 # that time (at least tests suggest so ;)
623 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
624 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
627 $self->$demolish($Mouse::Util::in_global_destruction);
634 die $e if $e; # rethrow
641 return unless $self->can('BUILD');
643 for my $class (reverse $self->meta->linearized_isa) {
644 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
653 *DEMOLISHALL = \&DESTROY;
660 Mouse::PurePerl - A Mouse guts in pure Perl
664 This document describes Mouse version 0.50_02