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]+$/ }
141 sub Str { defined($_[0]) && !ref($_[0]) }
143 sub Ref { ref($_[0]) }
144 sub ScalarRef { ref($_[0]) eq 'SCALAR' }
145 sub ArrayRef { ref($_[0]) eq 'ARRAY' }
146 sub HashRef { ref($_[0]) eq 'HASH' }
147 sub CodeRef { ref($_[0]) eq 'CODE' }
148 sub RegexpRef { ref($_[0]) eq 'Regexp' }
149 sub GlobRef { ref($_[0]) eq 'GLOB' }
152 openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
155 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
157 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
158 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
160 sub _parameterize_ArrayRef_for {
161 my($type_parameter) = @_;
162 my $check = $type_parameter->_compiled_type_constraint;
165 foreach my $value (@{$_}) {
166 return undef unless $check->($value);
172 sub _parameterize_HashRef_for {
173 my($type_parameter) = @_;
174 my $check = $type_parameter->_compiled_type_constraint;
177 foreach my $value(values %{$_}){
178 return undef unless $check->($value);
184 # 'Maybe' type accepts 'Any', so it requires parameters
185 sub _parameterize_Maybe_for {
186 my($type_parameter) = @_;
187 my $check = $type_parameter->_compiled_type_constraint;
190 return !defined($_) || $check->($_);
199 sub name { $_[0]->{package} }
201 sub _method_map { $_[0]->{methods} }
202 sub _attribute_map{ $_[0]->{attributes} }
205 my $name = $_[0]->{package};
207 return \%{ $name . '::' };
211 my($self, $name, $code) = @_;
214 $self->throw_error('You must pass a defined name');
217 $self->throw_error('You must pass a defined code');
220 if(ref($code) ne 'CODE'){
221 $code = \&{$code}; # coerce
224 $self->{methods}->{$name} = $code; # Moose stores meta object here.
226 my $pkg = $self->name;
228 no warnings 'redefine', 'once';
229 *{ $pkg . '::' . $name } = $code;
236 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
237 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
239 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
240 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
243 return exists $_[0]->{anon_serial_id};
246 sub roles { $_[0]->{roles} }
248 sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
250 sub get_all_attributes {
252 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
253 return values %attrs;
258 my %args = (@_ == 1 ? %{$_[0]} : @_);
260 my $object = bless {}, $self->name;
262 $self->_initialize_object($object, \%args);
266 sub _initialize_object{
267 my($self, $object, $args, $ignore_triggers) = @_;
271 foreach my $attribute ($self->get_all_attributes) {
272 my $init_arg = $attribute->init_arg;
273 my $slot = $attribute->name;
275 if (defined($init_arg) && exists($args->{$init_arg})) {
276 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
278 weaken($object->{$slot})
279 if ref($object->{$slot}) && $attribute->is_weak_ref;
281 if ($attribute->has_trigger) {
282 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
286 if ($attribute->has_default || $attribute->has_builder) {
287 if (!$attribute->is_lazy) {
288 my $default = $attribute->default;
289 my $builder = $attribute->builder;
290 my $value = $builder ? $object->$builder()
291 : ref($default) eq 'CODE' ? $object->$default()
294 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
296 weaken($object->{$slot})
297 if ref($object->{$slot}) && $attribute->is_weak_ref;
300 elsif($attribute->is_required) {
301 $self->throw_error("Attribute (".$attribute->name.") is required");
306 if(!$ignore_triggers){
307 foreach my $trigger_and_value(@triggers_queue){
308 my($trigger, $value) = @{$trigger_and_value};
309 $trigger->($object, $value);
313 if($self->is_anon_class){
314 $object->{__METACLASS__} = $self;
320 sub is_immutable { $_[0]->{is_immutable} }
325 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
328 return exists $_[0]->{anon_serial_id};
331 sub get_roles { $_[0]->{roles} }
334 Mouse::Meta::Attribute;
336 require Mouse::Meta::Method::Accessor;
338 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
342 sub name { $_[0]->{name} }
343 sub associated_class { $_[0]->{associated_class} }
345 sub accessor { $_[0]->{accessor} }
346 sub reader { $_[0]->{reader} }
347 sub writer { $_[0]->{writer} }
348 sub predicate { $_[0]->{predicate} }
349 sub clearer { $_[0]->{clearer} }
350 sub handles { $_[0]->{handles} }
352 sub _is_metadata { $_[0]->{is} }
353 sub is_required { $_[0]->{required} }
354 sub default { $_[0]->{default} }
355 sub is_lazy { $_[0]->{lazy} }
356 sub is_lazy_build { $_[0]->{lazy_build} }
357 sub is_weak_ref { $_[0]->{weak_ref} }
358 sub init_arg { $_[0]->{init_arg} }
359 sub type_constraint { $_[0]->{type_constraint} }
361 sub trigger { $_[0]->{trigger} }
362 sub builder { $_[0]->{builder} }
363 sub should_auto_deref { $_[0]->{auto_deref} }
364 sub should_coerce { $_[0]->{coerce} }
366 sub documentation { $_[0]->{documentation} }
370 sub has_accessor { exists $_[0]->{accessor} }
371 sub has_reader { exists $_[0]->{reader} }
372 sub has_writer { exists $_[0]->{writer} }
373 sub has_predicate { exists $_[0]->{predicate} }
374 sub has_clearer { exists $_[0]->{clearer} }
375 sub has_handles { exists $_[0]->{handles} }
377 sub has_default { exists $_[0]->{default} }
378 sub has_type_constraint { exists $_[0]->{type_constraint} }
379 sub has_trigger { exists $_[0]->{trigger} }
380 sub has_builder { exists $_[0]->{builder} }
382 sub has_documentation { exists $_[0]->{documentation} }
385 Mouse::Meta::TypeConstraint;
387 sub name { $_[0]->{name} }
388 sub parent { $_[0]->{parent} }
389 sub message { $_[0]->{message} }
391 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
393 sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
395 sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
398 sub compile_type_constraint{
403 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
404 if($parent->{hand_optimized_type_constraint}){
405 unshift @checks, $parent->{hand_optimized_type_constraint};
406 last; # a hand optimized constraint must include all the parents
408 elsif($parent->{constraint}){
409 unshift @checks, $parent->{constraint};
414 if($self->{constraint}){
415 push @checks, $self->{constraint};
418 if($self->{type_constraints}){ # Union
419 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
421 foreach my $c(@types){
422 return 1 if $c->($_[0]);
429 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
432 $self->{compiled_type_constraint} = sub{
435 foreach my $c(@checks){
436 return undef if !$c->(@args);
451 if (scalar @_ == 1) {
452 (ref($_[0]) eq 'HASH')
453 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
465 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
467 my $args = $class->BUILDARGS(@_);
469 my $meta = Mouse::Meta::Class->initialize($class);
470 my $self = $meta->new_object($args);
473 if( $self->can('BUILD') ) {
474 for my $class (reverse $meta->linearized_isa) {
475 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
478 $self->$build($args);
488 return unless $self->can('DEMOLISH'); # short circuit
498 # We cannot count on being able to retrieve a previously made
499 # metaclass, _or_ being able to make a new one during global
500 # destruction. However, we should still be able to use mro at
501 # that time (at least tests suggest so ;)
503 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
504 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
514 die $e if $e; # rethrow
521 return unless $self->can('BUILD');
523 for my $class (reverse $self->meta->linearized_isa) {
524 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
533 *DEMOLISHALL = \&DESTROY;
540 Mouse::PurePerl - A Mouse guts in pure Perl
544 This document describes Mouse version 0.45