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::"}} == \*main::Foo::
24 foreach my $part (split('::', $class)) {
25 my $entry = \$pack->{$part . '::'};
26 return 0 if ref($entry) ne 'GLOB';
27 $pack = *{$entry}{HASH} or return 0;
30 # check for $VERSION or @ISA
31 return 1 if exists $pack->{VERSION}
32 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
33 return 1 if exists $pack->{ISA}
34 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
36 # check for any method
37 foreach my $name( keys %{$pack} ) {
38 my $entry = \$pack->{$name};
39 return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
47 # taken from Sub::Identify
50 ref($coderef) or return;
52 my $cv = B::svref_2object($coderef);
53 $cv->isa('B::CV') or return;
56 $gv->isa('B::GV') or return;
58 return ($gv->STASH->NAME, $gv->NAME);
64 my $cv = B::svref_2object($coderef);
65 $cv->isa('B::CV') or return '';
68 $gv->isa('B::GV') or return '';
70 return $gv->STASH->NAME;
74 my($package, $name) = @_;
77 use warnings FATAL => 'uninitialized';
78 return *{$package . '::' . $name}{CODE};
81 sub generate_isa_predicate_for {
82 my($for_class, $name) = @_;
84 my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
88 *{ caller() . '::' . $name } = $predicate;
97 Mouse::Util::TypeConstraints;
99 use Scalar::Util qw(blessed looks_like_number openhandle);
104 sub Bool { $_[0] ? $_[0] eq '1' : 1 }
105 sub Undef { !defined($_[0]) }
106 sub Defined { defined($_[0]) }
107 sub Value { defined($_[0]) && !ref($_[0]) }
108 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
109 sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
110 sub Str { defined($_[0]) && !ref($_[0]) }
112 sub Ref { ref($_[0]) }
113 sub ScalarRef { ref($_[0]) eq 'SCALAR' }
114 sub ArrayRef { ref($_[0]) eq 'ARRAY' }
115 sub HashRef { ref($_[0]) eq 'HASH' }
116 sub CodeRef { ref($_[0]) eq 'CODE' }
117 sub RegexpRef { ref($_[0]) eq 'Regexp' }
118 sub GlobRef { ref($_[0]) eq 'GLOB' }
121 openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
124 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
126 sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
127 sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
129 sub _parameterize_ArrayRef_for {
130 my($type_parameter) = @_;
131 my $check = $type_parameter->_compiled_type_constraint;
134 foreach my $value (@{$_}) {
135 return undef unless $check->($value);
141 sub _parameterize_HashRef_for {
142 my($type_parameter) = @_;
143 my $check = $type_parameter->_compiled_type_constraint;
146 foreach my $value(values %{$_}){
147 return undef unless $check->($value);
153 # 'Maybe' type accepts 'Any', so it requires parameters
154 sub _parameterize_Maybe_for {
155 my($type_parameter) = @_;
156 my $check = $type_parameter->_compiled_type_constraint;
159 return !defined($_) || $check->($_);
168 sub name { $_[0]->{package} }
170 sub _method_map { $_[0]->{methods} }
171 sub _attribute_map{ $_[0]->{attributes} }
174 my $name = $_[0]->{package};
176 return \%{ $name . '::' };
180 my($self, $name, $code) = @_;
183 $self->throw_error('You must pass a defined name');
186 $self->throw_error('You must pass a defined code');
189 if(ref($code) ne 'CODE'){
190 $code = \&{$code}; # coerce
193 $self->{methods}->{$name} = $code; # Moose stores meta object here.
195 my $pkg = $self->name;
197 no warnings 'redefine', 'once';
198 *{ $pkg . '::' . $name } = $code;
206 sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
207 sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
209 sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
210 sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
213 return exists $_[0]->{anon_serial_id};
216 sub roles { $_[0]->{roles} }
218 sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
220 sub get_all_attributes {
222 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
223 return values %attrs;
228 my %args = (@_ == 1 ? %{$_[0]} : @_);
230 my $object = bless {}, $self->name;
232 $self->_initialize_object($object, \%args);
236 sub _initialize_object{
237 my($self, $object, $args, $ignore_triggers) = @_;
241 foreach my $attribute ($self->get_all_attributes) {
242 my $init_arg = $attribute->init_arg;
243 my $slot = $attribute->name;
245 if (defined($init_arg) && exists($args->{$init_arg})) {
246 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
248 weaken($object->{$slot})
249 if ref($object->{$slot}) && $attribute->is_weak_ref;
251 if ($attribute->has_trigger) {
252 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
256 if ($attribute->has_default || $attribute->has_builder) {
257 if (!$attribute->is_lazy) {
258 my $default = $attribute->default;
259 my $builder = $attribute->builder;
260 my $value = $builder ? $object->$builder()
261 : ref($default) eq 'CODE' ? $object->$default()
264 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
266 weaken($object->{$slot})
267 if ref($object->{$slot}) && $attribute->is_weak_ref;
270 elsif($attribute->is_required) {
271 $self->throw_error("Attribute (".$attribute->name.") is required");
276 if(!$ignore_triggers){
277 foreach my $trigger_and_value(@triggers_queue){
278 my($trigger, $value) = @{$trigger_and_value};
279 $trigger->($object, $value);
283 if($self->is_anon_class){
284 $object->{__METACLASS__} = $self;
294 sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
297 return exists $_[0]->{anon_serial_id};
300 sub get_roles { $_[0]->{roles} }
303 Mouse::Meta::Attribute;
305 require Mouse::Meta::Method::Accessor;
307 sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
311 sub name { $_[0]->{name} }
312 sub associated_class { $_[0]->{associated_class} }
314 sub accessor { $_[0]->{accessor} }
315 sub reader { $_[0]->{reader} }
316 sub writer { $_[0]->{writer} }
317 sub predicate { $_[0]->{predicate} }
318 sub clearer { $_[0]->{clearer} }
319 sub handles { $_[0]->{handles} }
321 sub _is_metadata { $_[0]->{is} }
322 sub is_required { $_[0]->{required} }
323 sub default { $_[0]->{default} }
324 sub is_lazy { $_[0]->{lazy} }
325 sub is_lazy_build { $_[0]->{lazy_build} }
326 sub is_weak_ref { $_[0]->{weak_ref} }
327 sub init_arg { $_[0]->{init_arg} }
328 sub type_constraint { $_[0]->{type_constraint} }
330 sub trigger { $_[0]->{trigger} }
331 sub builder { $_[0]->{builder} }
332 sub should_auto_deref { $_[0]->{auto_deref} }
333 sub should_coerce { $_[0]->{coerce} }
335 sub documentation { $_[0]->{documentation} }
339 sub has_accessor { exists $_[0]->{accessor} }
340 sub has_reader { exists $_[0]->{reader} }
341 sub has_writer { exists $_[0]->{writer} }
342 sub has_predicate { exists $_[0]->{predicate} }
343 sub has_clearer { exists $_[0]->{clearer} }
344 sub has_handles { exists $_[0]->{handles} }
346 sub has_default { exists $_[0]->{default} }
347 sub has_type_constraint { exists $_[0]->{type_constraint} }
348 sub has_trigger { exists $_[0]->{trigger} }
349 sub has_builder { exists $_[0]->{builder} }
351 sub has_documentation { exists $_[0]->{documentation} }
354 Mouse::Meta::TypeConstraint;
356 sub name { $_[0]->{name} }
357 sub parent { $_[0]->{parent} }
358 sub message { $_[0]->{message} }
360 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
362 sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
364 sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
367 sub compile_type_constraint{
372 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
373 if($parent->{hand_optimized_type_constraint}){
374 unshift @checks, $parent->{hand_optimized_type_constraint};
375 last; # a hand optimized constraint must include all the parents
377 elsif($parent->{constraint}){
378 unshift @checks, $parent->{constraint};
383 if($self->{constraint}){
384 push @checks, $self->{constraint};
387 if($self->{type_constraints}){ # Union
388 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
390 foreach my $c(@types){
391 return 1 if $c->($_[0]);
398 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
401 $self->{compiled_type_constraint} = sub{
404 foreach my $c(@checks){
405 return undef if !$c->(@args);
420 if (scalar @_ == 1) {
421 (ref($_[0]) eq 'HASH')
422 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
434 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
436 my $args = $class->BUILDARGS(@_);
438 my $meta = Mouse::Meta::Class->initialize($class);
439 my $self = $meta->new_object($args);
442 if( $self->can('BUILD') ) {
443 for my $class (reverse $meta->linearized_isa) {
444 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
447 $self->$build($args);
457 return unless $self->can('DEMOLISH'); # short circuit
467 # We cannot count on being able to retrieve a previously made
468 # metaclass, _or_ being able to make a new one during global
469 # destruction. However, we should still be able to use mro at
470 # that time (at least tests suggest so ;)
472 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
473 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
483 die $e if $e; # rethrow
491 Mouse::PurePerl - A Mouse guts in pure Perl
495 This document describes Mouse version 0.40_06