Remove has_package_symbol and add_package_symbol
[gitmo/Mouse.git] / lib / Mouse / PurePerl.pm
CommitLineData
ccb38d0b 1package Mouse::PurePerl;
2
a52cca04 3require Mouse::Util;
4
df6dd016 5package
6 Mouse::Util;
7
8use strict;
9use warnings;
10
11use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl
12
13use B ();
14
15sub is_class_loaded {
16 my $class = shift;
17
18 return 0 if ref($class) || !defined($class) || !length($class);
19
20 # walk the symbol table tree to avoid autovififying
3bb9e54e 21 # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::
df6dd016 22
23 my $pack = \%::;
24 foreach my $part (split('::', $class)) {
3bb9e54e 25 $part .= '::';
26 return 0 if !exists $pack->{$part};
27
28 my $entry = \$pack->{$part};
df6dd016 29 return 0 if ref($entry) ne 'GLOB';
3bb9e54e 30 $pack = *{$entry}{HASH};
df6dd016 31 }
32
3bb9e54e 33 return 0 if !%{$pack};
34
df6dd016 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;
40
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};
45 }
46
47 # fail
48 return 0;
49}
50
51
52# taken from Sub::Identify
53sub get_code_info {
54 my ($coderef) = @_;
55 ref($coderef) or return;
56
57 my $cv = B::svref_2object($coderef);
58 $cv->isa('B::CV') or return;
59
60 my $gv = $cv->GV;
61 $gv->isa('B::GV') or return;
62
63 return ($gv->STASH->NAME, $gv->NAME);
64}
65
66sub get_code_package{
67 my($coderef) = @_;
68
69 my $cv = B::svref_2object($coderef);
70 $cv->isa('B::CV') or return '';
71
72 my $gv = $cv->GV;
73 $gv->isa('B::GV') or return '';
74
75 return $gv->STASH->NAME;
76}
77
7d96ae4d 78sub get_code_ref{
79 my($package, $name) = @_;
80 no strict 'refs';
81 no warnings 'once';
82 use warnings FATAL => 'uninitialized';
83 return *{$package . '::' . $name}{CODE};
84}
85
e3540312 86sub generate_isa_predicate_for {
1d5ecd5f 87 my($for_class, $name) = @_;
88
f48920c1 89 my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
1d5ecd5f 90
91 if(defined $name){
92 no strict 'refs';
93 *{ caller() . '::' . $name } = $predicate;
94 return;
95 }
96
97 return $predicate;
98}
99
100
f48920c1 101package
102 Mouse::Util::TypeConstraints;
103
104use Scalar::Util qw(blessed looks_like_number openhandle);
105
7d96ae4d 106sub Any { 1 }
107sub Item { 1 }
7d96ae4d 108
109sub Bool { $_[0] ? $_[0] eq '1' : 1 }
110sub Undef { !defined($_[0]) }
111sub Defined { defined($_[0]) }
112sub Value { defined($_[0]) && !ref($_[0]) }
113sub Num { !ref($_[0]) && looks_like_number($_[0]) }
114sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
115sub Str { defined($_[0]) && !ref($_[0]) }
116
117sub Ref { ref($_[0]) }
118sub ScalarRef { ref($_[0]) eq 'SCALAR' }
119sub ArrayRef { ref($_[0]) eq 'ARRAY' }
120sub HashRef { ref($_[0]) eq 'HASH' }
121sub CodeRef { ref($_[0]) eq 'CODE' }
122sub RegexpRef { ref($_[0]) eq 'Regexp' }
123sub GlobRef { ref($_[0]) eq 'GLOB' }
124
125sub FileHandle {
126 openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
127}
128
129sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
130
131sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
132sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
133
619338ac 134sub _parameterize_ArrayRef_for {
135 my($type_parameter) = @_;
136 my $check = $type_parameter->_compiled_type_constraint;
137
138 return sub {
139 foreach my $value (@{$_}) {
140 return undef unless $check->($value);
141 }
142 return 1;
143 }
144}
145
146sub _parameterize_HashRef_for {
147 my($type_parameter) = @_;
148 my $check = $type_parameter->_compiled_type_constraint;
149
150 return sub {
151 foreach my $value(values %{$_}){
152 return undef unless $check->($value);
153 }
154 return 1;
155 };
156}
157
158# 'Maybe' type accepts 'Any', so it requires parameters
159sub _parameterize_Maybe_for {
160 my($type_parameter) = @_;
161 my $check = $type_parameter->_compiled_type_constraint;
162
163 return sub{
164 return !defined($_) || $check->($_);
165 };
166};
167
168
7d96ae4d 169
43165725 170package
171 Mouse::Meta::Module;
172
926404f2 173sub name { $_[0]->{package} }
174
175sub _method_map { $_[0]->{methods} }
c5f6ad05 176sub _attribute_map{ $_[0]->{attributes} }
43165725 177
2591e962 178sub namespace{
179 my $name = $_[0]->{package};
180 no strict 'refs';
181 return \%{ $name . '::' };
182}
183
3e44140b 184sub add_method {
185 my($self, $name, $code) = @_;
186
187 if(!defined $name){
188 $self->throw_error('You must pass a defined name');
189 }
190 if(!defined $code){
191 $self->throw_error('You must pass a defined code');
192 }
193
194 if(ref($code) ne 'CODE'){
195 $code = \&{$code}; # coerce
196 }
197
198 $self->{methods}->{$name} = $code; # Moose stores meta object here.
199
200 my $pkg = $self->name;
201 no strict 'refs';
202 no warnings 'redefine', 'once';
203 *{ $pkg . '::' . $name } = $code;
204 return;
205}
206
43165725 207package
208 Mouse::Meta::Class;
209
e058b279 210sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
211sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
212
213sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
214sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
4e7e3250 215
43165725 216sub is_anon_class{
217 return exists $_[0]->{anon_serial_id};
218}
219
220sub roles { $_[0]->{roles} }
221
cccb83de 222sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
223
da4432f3 224sub get_all_attributes {
225 my($self) = @_;
047d7af0 226 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
da4432f3 227 return values %attrs;
228}
229
ba153b33 230sub new_object {
231 my $self = shift;
232 my %args = (@_ == 1 ? %{$_[0]} : @_);
233
234 my $object = bless {}, $self->name;
235
236 $self->_initialize_object($object, \%args);
237 return $object;
238}
239
4e7e3250 240sub _initialize_object{
241 my($self, $object, $args, $ignore_triggers) = @_;
242
243 my @triggers_queue;
244
245 foreach my $attribute ($self->get_all_attributes) {
246 my $init_arg = $attribute->init_arg;
247 my $slot = $attribute->name;
248
249 if (defined($init_arg) && exists($args->{$init_arg})) {
250 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
251
252 weaken($object->{$slot})
253 if ref($object->{$slot}) && $attribute->is_weak_ref;
254
255 if ($attribute->has_trigger) {
256 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
257 }
258 }
259 else { # no init arg
260 if ($attribute->has_default || $attribute->has_builder) {
261 if (!$attribute->is_lazy) {
262 my $default = $attribute->default;
263 my $builder = $attribute->builder;
264 my $value = $builder ? $object->$builder()
265 : ref($default) eq 'CODE' ? $object->$default()
266 : $default;
267
268 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
269
270 weaken($object->{$slot})
271 if ref($object->{$slot}) && $attribute->is_weak_ref;
272 }
273 }
274 elsif($attribute->is_required) {
275 $self->throw_error("Attribute (".$attribute->name.") is required");
276 }
277 }
278 }
279
280 if(!$ignore_triggers){
281 foreach my $trigger_and_value(@triggers_queue){
282 my($trigger, $value) = @{$trigger_and_value};
283 $trigger->($object, $value);
284 }
285 }
286
287 if($self->is_anon_class){
288 $object->{__METACLASS__} = $self;
289 }
290
291 return;
292}
293
294
43165725 295package
296 Mouse::Meta::Role;
297
e058b279 298sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
299
43165725 300sub is_anon_role{
301 return exists $_[0]->{anon_serial_id};
302}
303
304sub get_roles { $_[0]->{roles} }
305
306package
307 Mouse::Meta::Attribute;
308
e058b279 309require Mouse::Meta::Method::Accessor;
310
311sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
43165725 312
313# readers
314
315sub name { $_[0]->{name} }
316sub associated_class { $_[0]->{associated_class} }
317
318sub accessor { $_[0]->{accessor} }
319sub reader { $_[0]->{reader} }
320sub writer { $_[0]->{writer} }
321sub predicate { $_[0]->{predicate} }
322sub clearer { $_[0]->{clearer} }
323sub handles { $_[0]->{handles} }
324
325sub _is_metadata { $_[0]->{is} }
326sub is_required { $_[0]->{required} }
327sub default { $_[0]->{default} }
328sub is_lazy { $_[0]->{lazy} }
329sub is_lazy_build { $_[0]->{lazy_build} }
330sub is_weak_ref { $_[0]->{weak_ref} }
331sub init_arg { $_[0]->{init_arg} }
332sub type_constraint { $_[0]->{type_constraint} }
333
334sub trigger { $_[0]->{trigger} }
335sub builder { $_[0]->{builder} }
336sub should_auto_deref { $_[0]->{auto_deref} }
337sub should_coerce { $_[0]->{coerce} }
338
d899d3e7 339sub documentation { $_[0]->{documentation} }
340
43165725 341# predicates
342
343sub has_accessor { exists $_[0]->{accessor} }
344sub has_reader { exists $_[0]->{reader} }
345sub has_writer { exists $_[0]->{writer} }
346sub has_predicate { exists $_[0]->{predicate} }
347sub has_clearer { exists $_[0]->{clearer} }
348sub has_handles { exists $_[0]->{handles} }
349
350sub has_default { exists $_[0]->{default} }
351sub has_type_constraint { exists $_[0]->{type_constraint} }
352sub has_trigger { exists $_[0]->{trigger} }
353sub has_builder { exists $_[0]->{builder} }
354
d899d3e7 355sub has_documentation { exists $_[0]->{documentation} }
356
43165725 357package
358 Mouse::Meta::TypeConstraint;
359
360sub name { $_[0]->{name} }
361sub parent { $_[0]->{parent} }
362sub message { $_[0]->{message} }
363
364sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
365
93540011 366sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
df6dd016 367
93540011 368sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
df6dd016 369
f790c46b 370
371sub compile_type_constraint{
372 my($self) = @_;
373
374 # add parents first
375 my @checks;
376 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
377 if($parent->{hand_optimized_type_constraint}){
378 unshift @checks, $parent->{hand_optimized_type_constraint};
379 last; # a hand optimized constraint must include all the parents
380 }
381 elsif($parent->{constraint}){
382 unshift @checks, $parent->{constraint};
383 }
384 }
385
386 # then add child
387 if($self->{constraint}){
388 push @checks, $self->{constraint};
389 }
390
391 if($self->{type_constraints}){ # Union
392 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
393 push @checks, sub{
394 foreach my $c(@types){
395 return 1 if $c->($_[0]);
396 }
397 return 0;
398 };
399 }
400
401 if(@checks == 0){
402 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
403 }
404 else{
405 $self->{compiled_type_constraint} = sub{
406 my(@args) = @_;
407 local $_ = $args[0];
408 foreach my $c(@checks){
409 return undef if !$c->(@args);
410 }
411 return 1;
412 };
413 }
414 return;
415}
416
aa2d2e2c 417package
418 Mouse::Object;
419
420
421sub BUILDARGS {
422 my $class = shift;
f790c46b 423
aa2d2e2c 424 if (scalar @_ == 1) {
425 (ref($_[0]) eq 'HASH')
426 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
427
428 return {%{$_[0]}};
429 }
430 else {
431 return {@_};
432 }
433}
f790c46b 434
af04626d 435sub new {
436 my $class = shift;
437
438 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
439
440 my $args = $class->BUILDARGS(@_);
441
442 my $meta = Mouse::Meta::Class->initialize($class);
443 my $self = $meta->new_object($args);
444
445 # BUILDALL
446 if( $self->can('BUILD') ) {
447 for my $class (reverse $meta->linearized_isa) {
448 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
449 || next;
450
451 $self->$build($args);
452 }
453 }
454
455 return $self;
456}
457
a5c683f6 458sub DESTROY {
459 my $self = shift;
460
461 return unless $self->can('DEMOLISH'); # short circuit
462
463 local $?;
464
465 my $e = do{
466 local $@;
467 eval{
468
469 # DEMOLISHALL
470
471 # We cannot count on being able to retrieve a previously made
472 # metaclass, _or_ being able to make a new one during global
473 # destruction. However, we should still be able to use mro at
474 # that time (at least tests suggest so ;)
475
476 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
477 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
478 || next;
479
480 $self->$demolish();
481 }
482 };
483 $@;
484 };
485
486 no warnings 'misc';
487 die $e if $e; # rethrow
488}
489
df6dd016 4901;
491__END__
ccb38d0b 492
493=head1 NAME
494
495Mouse::PurePerl - A Mouse guts in pure Perl
496
497=head1 VERSION
498
5176a3e4 499This document describes Mouse version 0.42
ccb38d0b 500
501=head1 SEE ALSO
502
503L<Mouse::XS>
504
505=cut