Checking in changes prior to tagging of version 0.41. Changelog diff is:
[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
f18345f1 207my %SIGIL_MAP = (
208 '$' => 'SCALAR',
209 '@' => 'ARRAY',
210 '%' => 'HASH',
211 '&' => 'CODE',
212 '*' => 'GLOB',
213);
214
215sub _deconstruct_variable_name {
216 my($self, $variable) = @_;
217
218 (defined $variable)
219 || $self->throw_error("You must pass a variable name");
220
221 my $sigil = substr($variable, 0, 1, '');
222
223 (defined $sigil)
224 || $self->throw_error("The variable name must include a sigil");
225
226 (exists $SIGIL_MAP{$sigil})
227 || $self->throw_error("I do not recognize that sigil '$sigil'");
228
229 return ($variable, $SIGIL_MAP{$sigil});
230}
231
232sub has_package_symbol {
233 my($self, $variable) = @_;
234
235 my($name, $type) = $self->_deconstruct_variable_name($variable);
236
237 my $namespace = $self->namespace;
238
239 return 0 unless exists $namespace->{$name};
240
241 my $entry_ref = \$namespace->{$name};
242 if ( ref($entry_ref) eq 'GLOB' ) {
243 return defined( *{$entry_ref}{$type} );
244 }
245 else {
246 # a symbol table entry can be -1 (stub), string (stub with prototype),
247 # or reference (constant)
248 return $type eq 'CODE';
249 }
250}
251
252sub get_package_symbol {
253 my ($self, $variable) = @_;
254
255 my($name, $type) = $self->_deconstruct_variable_name($variable);
256
257 my $namespace = $self->namespace;
258
259 return undef
260 unless exists $namespace->{$name};
261
262 my $entry_ref = \$namespace->{$name};
263
264 if ( ref($entry_ref) eq 'GLOB' ) {
265 return *{$entry_ref}{$type};
266 }
267 else {
268 if ( $type eq 'CODE' ) {
269 no strict 'refs';
270 return \&{ $self->name . '::' . $name };
271 }
272 else {
273 return undef;
274 }
275 }
276}
277
3e44140b 278
43165725 279package
280 Mouse::Meta::Class;
281
e058b279 282sub method_metaclass { $_[0]->{method_metaclass} || 'Mouse::Meta::Method' }
283sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mouse::Meta::Attribute' }
284
285sub constructor_class { $_[0]->{constructor_class} || 'Mouse::Meta::Method::Constructor' }
286sub destructor_class { $_[0]->{destructor_class} || 'Mouse::Meta::Method::Destructor' }
4e7e3250 287
43165725 288sub is_anon_class{
289 return exists $_[0]->{anon_serial_id};
290}
291
292sub roles { $_[0]->{roles} }
293
cccb83de 294sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
295
da4432f3 296sub get_all_attributes {
297 my($self) = @_;
047d7af0 298 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
da4432f3 299 return values %attrs;
300}
301
ba153b33 302sub new_object {
303 my $self = shift;
304 my %args = (@_ == 1 ? %{$_[0]} : @_);
305
306 my $object = bless {}, $self->name;
307
308 $self->_initialize_object($object, \%args);
309 return $object;
310}
311
4e7e3250 312sub _initialize_object{
313 my($self, $object, $args, $ignore_triggers) = @_;
314
315 my @triggers_queue;
316
317 foreach my $attribute ($self->get_all_attributes) {
318 my $init_arg = $attribute->init_arg;
319 my $slot = $attribute->name;
320
321 if (defined($init_arg) && exists($args->{$init_arg})) {
322 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
323
324 weaken($object->{$slot})
325 if ref($object->{$slot}) && $attribute->is_weak_ref;
326
327 if ($attribute->has_trigger) {
328 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
329 }
330 }
331 else { # no init arg
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()
338 : $default;
339
340 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
341
342 weaken($object->{$slot})
343 if ref($object->{$slot}) && $attribute->is_weak_ref;
344 }
345 }
346 elsif($attribute->is_required) {
347 $self->throw_error("Attribute (".$attribute->name.") is required");
348 }
349 }
350 }
351
352 if(!$ignore_triggers){
353 foreach my $trigger_and_value(@triggers_queue){
354 my($trigger, $value) = @{$trigger_and_value};
355 $trigger->($object, $value);
356 }
357 }
358
359 if($self->is_anon_class){
360 $object->{__METACLASS__} = $self;
361 }
362
363 return;
364}
365
366
43165725 367package
368 Mouse::Meta::Role;
369
e058b279 370sub method_metaclass{ $_[0]->{method_metaclass} || 'Mouse::Meta::Role::Method' }
371
43165725 372sub is_anon_role{
373 return exists $_[0]->{anon_serial_id};
374}
375
376sub get_roles { $_[0]->{roles} }
377
378package
379 Mouse::Meta::Attribute;
380
e058b279 381require Mouse::Meta::Method::Accessor;
382
383sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mouse::Meta::Method::Accessor' }
43165725 384
385# readers
386
387sub name { $_[0]->{name} }
388sub associated_class { $_[0]->{associated_class} }
389
390sub accessor { $_[0]->{accessor} }
391sub reader { $_[0]->{reader} }
392sub writer { $_[0]->{writer} }
393sub predicate { $_[0]->{predicate} }
394sub clearer { $_[0]->{clearer} }
395sub handles { $_[0]->{handles} }
396
397sub _is_metadata { $_[0]->{is} }
398sub is_required { $_[0]->{required} }
399sub default { $_[0]->{default} }
400sub is_lazy { $_[0]->{lazy} }
401sub is_lazy_build { $_[0]->{lazy_build} }
402sub is_weak_ref { $_[0]->{weak_ref} }
403sub init_arg { $_[0]->{init_arg} }
404sub type_constraint { $_[0]->{type_constraint} }
405
406sub trigger { $_[0]->{trigger} }
407sub builder { $_[0]->{builder} }
408sub should_auto_deref { $_[0]->{auto_deref} }
409sub should_coerce { $_[0]->{coerce} }
410
d899d3e7 411sub documentation { $_[0]->{documentation} }
412
43165725 413# predicates
414
415sub has_accessor { exists $_[0]->{accessor} }
416sub has_reader { exists $_[0]->{reader} }
417sub has_writer { exists $_[0]->{writer} }
418sub has_predicate { exists $_[0]->{predicate} }
419sub has_clearer { exists $_[0]->{clearer} }
420sub has_handles { exists $_[0]->{handles} }
421
422sub has_default { exists $_[0]->{default} }
423sub has_type_constraint { exists $_[0]->{type_constraint} }
424sub has_trigger { exists $_[0]->{trigger} }
425sub has_builder { exists $_[0]->{builder} }
426
d899d3e7 427sub has_documentation { exists $_[0]->{documentation} }
428
43165725 429package
430 Mouse::Meta::TypeConstraint;
431
432sub name { $_[0]->{name} }
433sub parent { $_[0]->{parent} }
434sub message { $_[0]->{message} }
435
436sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
437
93540011 438sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
df6dd016 439
93540011 440sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
df6dd016 441
f790c46b 442
443sub compile_type_constraint{
444 my($self) = @_;
445
446 # add parents first
447 my @checks;
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
452 }
453 elsif($parent->{constraint}){
454 unshift @checks, $parent->{constraint};
455 }
456 }
457
458 # then add child
459 if($self->{constraint}){
460 push @checks, $self->{constraint};
461 }
462
463 if($self->{type_constraints}){ # Union
464 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
465 push @checks, sub{
466 foreach my $c(@types){
467 return 1 if $c->($_[0]);
468 }
469 return 0;
470 };
471 }
472
473 if(@checks == 0){
474 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
475 }
476 else{
477 $self->{compiled_type_constraint} = sub{
478 my(@args) = @_;
479 local $_ = $args[0];
480 foreach my $c(@checks){
481 return undef if !$c->(@args);
482 }
483 return 1;
484 };
485 }
486 return;
487}
488
aa2d2e2c 489package
490 Mouse::Object;
491
492
493sub BUILDARGS {
494 my $class = shift;
f790c46b 495
aa2d2e2c 496 if (scalar @_ == 1) {
497 (ref($_[0]) eq 'HASH')
498 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
499
500 return {%{$_[0]}};
501 }
502 else {
503 return {@_};
504 }
505}
f790c46b 506
af04626d 507sub new {
508 my $class = shift;
509
510 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
511
512 my $args = $class->BUILDARGS(@_);
513
514 my $meta = Mouse::Meta::Class->initialize($class);
515 my $self = $meta->new_object($args);
516
517 # BUILDALL
518 if( $self->can('BUILD') ) {
519 for my $class (reverse $meta->linearized_isa) {
520 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
521 || next;
522
523 $self->$build($args);
524 }
525 }
526
527 return $self;
528}
529
a5c683f6 530sub DESTROY {
531 my $self = shift;
532
533 return unless $self->can('DEMOLISH'); # short circuit
534
535 local $?;
536
537 my $e = do{
538 local $@;
539 eval{
540
541 # DEMOLISHALL
542
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 ;)
547
548 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
549 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
550 || next;
551
552 $self->$demolish();
553 }
554 };
555 $@;
556 };
557
558 no warnings 'misc';
559 die $e if $e; # rethrow
560}
561
df6dd016 5621;
563__END__
ccb38d0b 564
565=head1 NAME
566
567Mouse::PurePerl - A Mouse guts in pure Perl
568
569=head1 VERSION
570
4c0fe06f 571This document describes Mouse version 0.41
ccb38d0b 572
573=head1 SEE ALSO
574
575L<Mouse::XS>
576
577=cut