1 package Mouse::PurePerl;
11 use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl
18 return 0 if ref($class) || !defined($class) || !length($class);
20 # walk the symbol table tree to avoid autovififying
21 # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::
24 foreach my $part (split('::', $class)) {
26 return 0 if !exists $pack->{$part};
28 my $entry = \$pack->{$part};
29 return 0 if ref($entry) ne 'GLOB';
30 $pack = *{$entry}{HASH};
33 return 0 if !%{$pack};
35 # check for $VERSION or @ISA
36 return 1 if exists $pack->{VERSION}
37 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
38 return 1 if exists $pack->{ISA}
39 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
41 # check for any method
42 foreach my $name( keys %{$pack} ) {
43 my $entry = \$pack->{$name};
44 return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
52 # taken from Sub::Identify
55 ref($coderef) or return;
57 my $cv = B::svref_2object($coderef);
58 $cv->isa('B::CV') or return;
61 $gv->isa('B::GV') or return;
63 return ($gv->STASH->NAME, $gv->NAME);
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;
79 my($package, $name) = @_;
82 use warnings FATAL => 'uninitialized';
83 return *{$package . '::' . $name}{CODE};
86 sub generate_isa_predicate_for {
87 my($for_class, $name) = @_;
89 my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
93 *{ caller() . '::' . $name } = $predicate;
100 sub generate_can_predicate_for {
101 my($methods_ref, $name) = @_;
103 my @methods = @{$methods_ref};
107 if(Scalar::Util::blessed($instance)){
108 foreach my $method(@methods){
109 if(!$instance->can($method)){
120 *{ caller() . '::' . $name } = $predicate;
128 Mouse::Util::TypeConstraints;
130 use Scalar::Util qw(blessed looks_like_number openhandle);
135 sub Bool { $_[0] ? $_[0] eq '1' : 1 }
136 sub Undef { !defined($_[0]) }
137 sub Defined { defined($_[0]) }
138 sub Value { defined($_[0]) && !ref($_[0]) }
139 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
140 sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
143 return defined($value) && ref(\$value) eq 'SCALAR';
146 sub Ref { ref($_[0]) }
149 return ref($value) eq 'SCALAR'
151 sub ArrayRef { ref($_[0]) eq 'ARRAY' }
152 sub HashRef { ref($_[0]) eq 'HASH' }
153 sub CodeRef { ref($_[0]) eq 'CODE' }
154 sub RegexpRef { ref($_[0]) eq 'Regexp' }
155 sub GlobRef { ref($_[0]) eq 'GLOB' }
158 return openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
161 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
163 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
164 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
166 sub _parameterize_ArrayRef_for {
167 my($type_parameter) = @_;
168 my $check = $type_parameter->_compiled_type_constraint;
171 foreach my $value (@{$_}) {
172 return undef unless $check->($value);
178 sub _parameterize_HashRef_for {
179 my($type_parameter) = @_;
180 my $check = $type_parameter->_compiled_type_constraint;
183 foreach my $value(values %{$_}){
184 return undef unless $check->($value);
190 # 'Maybe' type accepts 'Any', so it requires parameters
191 sub _parameterize_Maybe_for {
192 my($type_parameter) = @_;
193 my $check = $type_parameter->_compiled_type_constraint;
196 return !defined($_) || $check->($_);
205 sub name { $_[0]->{package} }
207 sub _method_map { $_[0]->{methods} }
208 sub _attribute_map{ $_[0]->{attributes} }
211 my $name = $_[0]->{package};
213 return \%{ $name . '::' };
217 my($self, $name, $code) = @_;
220 $self->throw_error('You must pass a defined name');
223 $self->throw_error('You must pass a defined code');
226 if(ref($code) ne 'CODE'){
227 $code = \&{$code}; # coerce
230 $self->{methods}->{$name} = $code; # Moose stores meta object here.
232 my $pkg = $self->name;
234 no warnings 'redefine', 'once';
235 *{ $pkg . '::' . $name } = $code;
242 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
243 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
245 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
246 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
249 return exists $_[0]->{anon_serial_id};
252 sub roles { $_[0]->{roles} }
254 sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
256 sub get_all_attributes {
258 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
259 return values %attrs;
264 my %args = (@_ == 1 ? %{$_[0]} : @_);
266 my $object = bless {}, $self->name;
268 $self->_initialize_object($object, \%args);
272 sub _initialize_object{
273 my($self, $object, $args, $ignore_triggers) = @_;
277 foreach my $attribute ($self->get_all_attributes) {
278 my $init_arg = $attribute->init_arg;
279 my $slot = $attribute->name;
281 if (defined($init_arg) && exists($args->{$init_arg})) {
282 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
284 weaken($object->{$slot})
285 if ref($object->{$slot}) && $attribute->is_weak_ref;
287 if ($attribute->has_trigger) {
288 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
292 if ($attribute->has_default || $attribute->has_builder) {
293 if (!$attribute->is_lazy) {
294 my $default = $attribute->default;
295 my $builder = $attribute->builder;
296 my $value = $builder ? $object->$builder()
297 : ref($default) eq 'CODE' ? $object->$default()
300 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
302 weaken($object->{$slot})
303 if ref($object->{$slot}) && $attribute->is_weak_ref;
306 elsif($attribute->is_required) {
307 $self->throw_error("Attribute (".$attribute->name.") is required");
312 if(!$ignore_triggers){
313 foreach my $trigger_and_value(@triggers_queue){
314 my($trigger, $value) = @{$trigger_and_value};
315 $trigger->($object, $value);
319 if($self->is_anon_class){
320 $object->{__METACLASS__} = $self;
326 sub is_immutable { $_[0]->{is_immutable} }
331 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
334 return exists $_[0]->{anon_serial_id};
337 sub get_roles { $_[0]->{roles} }
340 Mouse::Meta::Attribute;
342 require Mouse::Meta::Method::Accessor;
344 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
348 sub name { $_[0]->{name} }
349 sub associated_class { $_[0]->{associated_class} }
351 sub accessor { $_[0]->{accessor} }
352 sub reader { $_[0]->{reader} }
353 sub writer { $_[0]->{writer} }
354 sub predicate { $_[0]->{predicate} }
355 sub clearer { $_[0]->{clearer} }
356 sub handles { $_[0]->{handles} }
358 sub _is_metadata { $_[0]->{is} }
359 sub is_required { $_[0]->{required} }
360 sub default { $_[0]->{default} }
361 sub is_lazy { $_[0]->{lazy} }
362 sub is_lazy_build { $_[0]->{lazy_build} }
363 sub is_weak_ref { $_[0]->{weak_ref} }
364 sub init_arg { $_[0]->{init_arg} }
365 sub type_constraint { $_[0]->{type_constraint} }
367 sub trigger { $_[0]->{trigger} }
368 sub builder { $_[0]->{builder} }
369 sub should_auto_deref { $_[0]->{auto_deref} }
370 sub should_coerce { $_[0]->{coerce} }
372 sub documentation { $_[0]->{documentation} }
376 sub has_accessor { exists $_[0]->{accessor} }
377 sub has_reader { exists $_[0]->{reader} }
378 sub has_writer { exists $_[0]->{writer} }
379 sub has_predicate { exists $_[0]->{predicate} }
380 sub has_clearer { exists $_[0]->{clearer} }
381 sub has_handles { exists $_[0]->{handles} }
383 sub has_default { exists $_[0]->{default} }
384 sub has_type_constraint { exists $_[0]->{type_constraint} }
385 sub has_trigger { exists $_[0]->{trigger} }
386 sub has_builder { exists $_[0]->{builder} }
388 sub has_documentation { exists $_[0]->{documentation} }
390 sub _process_options{
391 my($class, $name, $args) = @_;
393 # taken from Class::MOP::Attribute::new
396 or $class->throw_error('You must provide a name for the attribute');
398 if(!exists $args->{init_arg}){
399 $args->{init_arg} = $name;
402 # 'required' requires eigher 'init_arg', 'builder', or 'default'
403 my $can_be_required = defined( $args->{init_arg} );
405 if(exists $args->{builder}){
407 # Moose refuses a CODE ref builder, but Mouse doesn't for backward compatibility
408 # This feature will be changed in a future. (gfx)
409 $class->throw_error('builder must be a defined scalar value which is a method name')
410 #if ref $args->{builder} || !defined $args->{builder};
411 if !defined $args->{builder};
415 elsif(exists $args->{default}){
416 if(ref $args->{default} && ref($args->{default}) ne 'CODE'){
417 $class->throw_error("References are not allowed as default values, you must "
418 . "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])");
423 if( $args->{required} && !$can_be_required ) {
424 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg");
427 # taken from Mouse::Meta::Attribute->new and ->_process_args
429 if(exists $args->{is}){
430 my $is = $args->{is};
433 $args->{reader} ||= $name;
436 if(exists $args->{writer}){
437 $args->{reader} ||= $name;
440 $args->{accessor} ||= $name;
443 elsif($is eq 'bare'){
444 # do nothing, but don't complain (later) about missing methods
447 $is = 'undef' if !defined $is;
448 $class->throw_error("I do not understand this option (is => $is) on attribute ($name)");
453 if(exists $args->{isa}){
454 $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($args->{isa});
456 elsif(exists $args->{does}){
457 $args->{type_constraint} = Mouse::Util::TypeConstraints::find_or_create_does_type_constraint($args->{does});
459 $tc = $args->{type_constraint};
463 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)");
466 && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)");
469 if ($args->{lazy_build}) {
470 exists($args->{default})
471 && $class->throw_error("You can not use lazy_build and default for the same attribute ($name)");
474 $args->{builder} ||= "_build_${name}";
476 $args->{clearer} ||= "_clear${name}";
477 $args->{predicate} ||= "_has${name}";
480 $args->{clearer} ||= "clear_${name}";
481 $args->{predicate} ||= "has_${name}";
485 if ($args->{auto_deref}) {
487 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)");
489 ( $tc->is_a_type_of('ArrayRef') || $tc->is_a_type_of('HashRef') )
490 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)");
493 if (exists $args->{trigger}) {
494 ('CODE' eq ref $args->{trigger})
495 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)");
499 (exists $args->{default} || defined $args->{builder})
500 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it");
508 Mouse::Meta::TypeConstraint;
510 sub name { $_[0]->{name} }
511 sub parent { $_[0]->{parent} }
512 sub message { $_[0]->{message} }
514 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
516 sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
518 sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
521 sub compile_type_constraint{
526 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
527 if($parent->{hand_optimized_type_constraint}){
528 unshift @checks, $parent->{hand_optimized_type_constraint};
529 last; # a hand optimized constraint must include all the parents
531 elsif($parent->{constraint}){
532 unshift @checks, $parent->{constraint};
537 if($self->{constraint}){
538 push @checks, $self->{constraint};
541 if($self->{type_constraints}){ # Union
542 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
544 foreach my $c(@types){
545 return 1 if $c->($_[0]);
552 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
555 $self->{compiled_type_constraint} = sub{
558 foreach my $c(@checks){
559 return undef if !$c->(@args);
574 if (scalar @_ == 1) {
575 (ref($_[0]) eq 'HASH')
576 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
588 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
590 my $args = $class->BUILDARGS(@_);
592 my $meta = Mouse::Meta::Class->initialize($class);
593 my $self = $meta->new_object($args);
596 if( $self->can('BUILD') ) {
597 for my $class (reverse $meta->linearized_isa) {
598 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
601 $self->$build($args);
611 return unless $self->can('DEMOLISH'); # short circuit
621 # We cannot count on being able to retrieve a previously made
622 # metaclass, _or_ being able to make a new one during global
623 # destruction. However, we should still be able to use mro at
624 # that time (at least tests suggest so ;)
626 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
627 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
637 die $e if $e; # rethrow
644 return unless $self->can('BUILD');
646 for my $class (reverse $self->meta->linearized_isa) {
647 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
656 *DEMOLISHALL = \&DESTROY;
663 Mouse::PurePerl - A Mouse guts in pure Perl
667 This document describes Mouse version 0.47