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;
102 Mouse::Util::TypeConstraints;
104 use Scalar::Util qw(blessed looks_like_number openhandle);
109 sub Bool { $_[0] ? $_[0] eq '1' : 1 }
110 sub Undef { !defined($_[0]) }
111 sub Defined { defined($_[0]) }
112 sub Value { defined($_[0]) && !ref($_[0]) }
113 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
114 sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
115 sub Str { defined($_[0]) && !ref($_[0]) }
117 sub Ref { ref($_[0]) }
118 sub ScalarRef { ref($_[0]) eq 'SCALAR' }
119 sub ArrayRef { ref($_[0]) eq 'ARRAY' }
120 sub HashRef { ref($_[0]) eq 'HASH' }
121 sub CodeRef { ref($_[0]) eq 'CODE' }
122 sub RegexpRef { ref($_[0]) eq 'Regexp' }
123 sub GlobRef { ref($_[0]) eq 'GLOB' }
126 openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
129 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
131 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
132 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
134 sub _parameterize_ArrayRef_for {
135 my($type_parameter) = @_;
136 my $check = $type_parameter->_compiled_type_constraint;
139 foreach my $value (@{$_}) {
140 return undef unless $check->($value);
146 sub _parameterize_HashRef_for {
147 my($type_parameter) = @_;
148 my $check = $type_parameter->_compiled_type_constraint;
151 foreach my $value(values %{$_}){
152 return undef unless $check->($value);
158 # 'Maybe' type accepts 'Any', so it requires parameters
159 sub _parameterize_Maybe_for {
160 my($type_parameter) = @_;
161 my $check = $type_parameter->_compiled_type_constraint;
164 return !defined($_) || $check->($_);
173 sub name { $_[0]->{package} }
175 sub _method_map { $_[0]->{methods} }
176 sub _attribute_map{ $_[0]->{attributes} }
179 my $name = $_[0]->{package};
181 return \%{ $name . '::' };
185 my($self, $name, $code) = @_;
188 $self->throw_error('You must pass a defined name');
191 $self->throw_error('You must pass a defined code');
194 if(ref($code) ne 'CODE'){
195 $code = \&{$code}; # coerce
198 $self->{methods}->{$name} = $code; # Moose stores meta object here.
200 my $pkg = $self->name;
202 no warnings 'redefine', 'once';
203 *{ $pkg . '::' . $name } = $code;
215 sub _deconstruct_variable_name {
216 my($self, $variable) = @_;
219 || $self->throw_error("You must pass a variable name");
221 my $sigil = substr($variable, 0, 1, '');
224 || $self->throw_error("The variable name must include a sigil");
226 (exists $SIGIL_MAP{$sigil})
227 || $self->throw_error("I do not recognize that sigil '$sigil'");
229 return ($variable, $SIGIL_MAP{$sigil});
232 sub has_package_symbol {
233 my($self, $variable) = @_;
235 my($name, $type) = $self->_deconstruct_variable_name($variable);
237 my $namespace = $self->namespace;
239 return 0 unless exists $namespace->{$name};
241 my $entry_ref = \$namespace->{$name};
242 if ( ref($entry_ref) eq 'GLOB' ) {
243 return defined( *{$entry_ref}{$type} );
246 # a symbol table entry can be -1 (stub), string (stub with prototype),
247 # or reference (constant)
248 return $type eq 'CODE';
252 sub get_package_symbol {
253 my ($self, $variable) = @_;
255 my($name, $type) = $self->_deconstruct_variable_name($variable);
257 my $namespace = $self->namespace;
260 unless exists $namespace->{$name};
262 my $entry_ref = \$namespace->{$name};
264 if ( ref($entry_ref) eq 'GLOB' ) {
265 return *{$entry_ref}{$type};
268 if ( $type eq 'CODE' ) {
270 return \&{ $self->name . '::' . $name };
282 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
283 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
285 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
286 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
289 return exists $_[0]->{anon_serial_id};
292 sub roles { $_[0]->{roles} }
294 sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
296 sub get_all_attributes {
298 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
299 return values %attrs;
304 my %args = (@_ == 1 ? %{$_[0]} : @_);
306 my $object = bless {}, $self->name;
308 $self->_initialize_object($object, \%args);
312 sub _initialize_object{
313 my($self, $object, $args, $ignore_triggers) = @_;
317 foreach my $attribute ($self->get_all_attributes) {
318 my $init_arg = $attribute->init_arg;
319 my $slot = $attribute->name;
321 if (defined($init_arg) && exists($args->{$init_arg})) {
322 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
324 weaken($object->{$slot})
325 if ref($object->{$slot}) && $attribute->is_weak_ref;
327 if ($attribute->has_trigger) {
328 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
332 if ($attribute->has_default || $attribute->has_builder) {
333 if (!$attribute->is_lazy) {
334 my $default = $attribute->default;
335 my $builder = $attribute->builder;
336 my $value = $builder ? $object->$builder()
337 : ref($default) eq 'CODE' ? $object->$default()
340 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
342 weaken($object->{$slot})
343 if ref($object->{$slot}) && $attribute->is_weak_ref;
346 elsif($attribute->is_required) {
347 $self->throw_error("Attribute (".$attribute->name.") is required");
352 if(!$ignore_triggers){
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;
370 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
373 return exists $_[0]->{anon_serial_id};
376 sub get_roles { $_[0]->{roles} }
379 Mouse::Meta::Attribute;
381 require Mouse::Meta::Method::Accessor;
383 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
387 sub name { $_[0]->{name} }
388 sub associated_class { $_[0]->{associated_class} }
390 sub accessor { $_[0]->{accessor} }
391 sub reader { $_[0]->{reader} }
392 sub writer { $_[0]->{writer} }
393 sub predicate { $_[0]->{predicate} }
394 sub clearer { $_[0]->{clearer} }
395 sub handles { $_[0]->{handles} }
397 sub _is_metadata { $_[0]->{is} }
398 sub is_required { $_[0]->{required} }
399 sub default { $_[0]->{default} }
400 sub is_lazy { $_[0]->{lazy} }
401 sub is_lazy_build { $_[0]->{lazy_build} }
402 sub is_weak_ref { $_[0]->{weak_ref} }
403 sub init_arg { $_[0]->{init_arg} }
404 sub type_constraint { $_[0]->{type_constraint} }
406 sub trigger { $_[0]->{trigger} }
407 sub builder { $_[0]->{builder} }
408 sub should_auto_deref { $_[0]->{auto_deref} }
409 sub should_coerce { $_[0]->{coerce} }
411 sub documentation { $_[0]->{documentation} }
415 sub has_accessor { exists $_[0]->{accessor} }
416 sub has_reader { exists $_[0]->{reader} }
417 sub has_writer { exists $_[0]->{writer} }
418 sub has_predicate { exists $_[0]->{predicate} }
419 sub has_clearer { exists $_[0]->{clearer} }
420 sub has_handles { exists $_[0]->{handles} }
422 sub has_default { exists $_[0]->{default} }
423 sub has_type_constraint { exists $_[0]->{type_constraint} }
424 sub has_trigger { exists $_[0]->{trigger} }
425 sub has_builder { exists $_[0]->{builder} }
427 sub has_documentation { exists $_[0]->{documentation} }
430 Mouse::Meta::TypeConstraint;
432 sub name { $_[0]->{name} }
433 sub parent { $_[0]->{parent} }
434 sub message { $_[0]->{message} }
436 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
438 sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
440 sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
443 sub compile_type_constraint{
448 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
449 if($parent->{hand_optimized_type_constraint}){
450 unshift @checks, $parent->{hand_optimized_type_constraint};
451 last; # a hand optimized constraint must include all the parents
453 elsif($parent->{constraint}){
454 unshift @checks, $parent->{constraint};
459 if($self->{constraint}){
460 push @checks, $self->{constraint};
463 if($self->{type_constraints}){ # Union
464 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
466 foreach my $c(@types){
467 return 1 if $c->($_[0]);
474 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
477 $self->{compiled_type_constraint} = sub{
480 foreach my $c(@checks){
481 return undef if !$c->(@args);
496 if (scalar @_ == 1) {
497 (ref($_[0]) eq 'HASH')
498 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
510 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
512 my $args = $class->BUILDARGS(@_);
514 my $meta = Mouse::Meta::Class->initialize($class);
515 my $self = $meta->new_object($args);
518 if( $self->can('BUILD') ) {
519 for my $class (reverse $meta->linearized_isa) {
520 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
523 $self->$build($args);
533 return unless $self->can('DEMOLISH'); # short circuit
543 # We cannot count on being able to retrieve a previously made
544 # metaclass, _or_ being able to make a new one during global
545 # destruction. However, we should still be able to use mro at
546 # that time (at least tests suggest so ;)
548 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
549 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
559 die $e if $e; # rethrow
567 Mouse::PurePerl - A Mouse guts in pure Perl
571 This document describes Mouse version 0.41