lib/Mousse.pm

filter: Statementstmt 25.5% 158 / 619 Branchbran 4.9% 12 / 242 Conditioncond 3.7% 5 / 133 MC/DCmcdc 0.0% 0 / 76 Subroutinesub 21.1% 33 / 156 Podpod 0.0% 0 / 99 totaltotal 15.7% 208 / 1325 CC 193 SCAR 56.3
CC
193
Cov
18%
_process_options
1892.0
do_import
342.0
is_class_loaded
306.0
CRAP 277.8
↑ Summary lib/Mousse/Maker.pm »
1 # This file was generated by Mousse::Maker 0.13 from Mouse 0.93.
2 #
3 # ANY CHANGES MADE HERE WILL BE LOST!
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # tell Perl we already have all of the Mousse files loaded:
8 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 $INC{'Mousse.pm'} = __FILE__;
10 3 $INC{'Mousse/Util.pm'} = __FILE__;
11 3 $INC{'Mousse/PurePerl.pm'} = __FILE__;
12 3 $INC{'Mousse/Role.pm'} = __FILE__;
13 3 $INC{'Mousse/Exporter.pm'} = __FILE__;
14 3 $INC{'Mousse/Object.pm'} = __FILE__;
15 3 $INC{'Mousse/Util/TypeConstraints.pm'} = __FILE__;
16 3 $INC{'Mousse/Util/MetaRole.pm'} = __FILE__;
17 3 $INC{'Mousse/Meta/Module.pm'} = __FILE__;
18 3 $INC{'Mousse/Meta/TypeConstraint.pm'} = __FILE__;
19 3 $INC{'Mousse/Meta/Method.pm'} = __FILE__;
20 3 $INC{'Mousse/Meta/Attribute.pm'} = __FILE__;
21 3 $INC{'Mousse/Meta/Role.pm'} = __FILE__;
22 3 $INC{'Mousse/Meta/Class.pm'} = __FILE__;
23 3 $INC{'Mousse/Meta/Method/Delegation.pm'} = __FILE__;
24 3 $INC{'Mousse/Meta/Method/Destructor.pm'} = __FILE__;
25 3 $INC{'Mousse/Meta/Method/Constructor.pm'} = __FILE__;
26 3 $INC{'Mousse/Meta/Method/Accessor.pm'} = __FILE__;
27 3 $INC{'Mousse/Meta/Role/Method.pm'} = __FILE__;
28 3 $INC{'Mousse/Meta/Role/Application.pm'} = __FILE__;
29 3 $INC{'Mousse/Meta/Role/Composite.pm'} = __FILE__;
30 }
31  
32 # and now their contents
33  
34 # Contents of Mouse::PurePerl
35 package Mousse::PurePerl;
36 # The pure Perl backend for Mousse
37 package Mousse::Util;
38 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use warnings FATAL => 'redefine'; # to avoid to load Mousse::PurePerl twice
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 require Mousse::Util;
46  
47 # taken from Class/MOP.pm
48 0 ▶sub is_valid_class_name {
Subroutine is_valid_class_name: not called CC=4 SCAR=30.0
Pod undocumented
49 0 my $class = shift;
50  
51 0 ▶ return 0 if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
52 0 ▶ return 0 unless defined($class);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 0 0
53  
54 0 ▶ return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms;
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /\A \w+ (?: :: \w+ )* \z/msx 0 0 0
55  
56 0 return 0;
57 }
58  
59 0 ▶sub is_class_loaded {
Subroutine is_class_loaded: not called CC=17 SCAR=57.2
Pod undocumented
60 0 my $class = shift;
61  
62 0 ▶ return 0 if ref($class) || !defined($class) || !length($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class or not defined $class or not length $class 0 0 0
Condition0%0/6
ref $class or not defined $class or not length $class
l!l&&r!l&&!r
000
ref $class or not defined $class
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $class or not defined $class or not length $class
ref $class not defined $class not length $class
Truth table0%0/4
A or not(B) or not(C)
A= ref $classB= not defined $classC= not length $class
ABCresult
1XX1
01X1
0011
0000
63  
64 # walk the symbol table tree to avoid autovififying
65 # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::
66  
67 0 my $pack = \%::;
68 0 foreach my $part (split('::', $class)) {
69 0 $part .= '::';
70 0 ▶ return 0 if !exists $pack->{$part};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pack->{$part} 0 0 0
71  
72 0 my $entry = \$pack->{$part};
73 0 ▶ return 0 if ref($entry) ne 'GLOB';
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry ne "GLOB" 0 0 0
74 0 $pack = *{$entry}{HASH};
75 }
76  
77 0 ▶ return 0 if !%{$pack};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$pack;} 0 0 0
78  
79 # check for $VERSION or @ISA
80 return 1 if exists $pack->{VERSION}
81 0 ▶ && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"} and defined ${$$pack{"VERSION"};} 0 0 0
Condition0%0/6
exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"} and defined ${$$pack{"VERSION"};}
!ll&&!rl&&r
000
exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"} and defined ${$$pack{"VERSION"};}
exists $pack->{"VERSION"} defined *{$pack->{"VERSION"};}{"SCALAR"} defined ${$$pack{"VERSION"};}
Truth table0%0/4
A and B and C
A= exists $pack->{"VERSION"}B= defined *{$pack->{"VERSION"};}{"SCALAR"}C= defined ${$$pack{"VERSION"};}
ABCresult
0XX0
10X0
1100
1111
82 return 1 if exists $pack->{ISA}
83 0 ▶ && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"} and @{$pack->{"ISA"};} != 0 0 0 0
Condition0%0/6
exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"} and @{$pack->{"ISA"};} != 0
!ll&&!rl&&r
000
exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"} and @{$pack->{"ISA"};} != 0
exists $pack->{"ISA"} defined *{$pack->{"ISA"};}{"ARRAY"} @{$pack->{"ISA"};} != 0
Truth table0%0/4
A and B and C
A= exists $pack->{"ISA"}B= defined *{$pack->{"ISA"};}{"ARRAY"}C= @{$pack->{"ISA"};} != 0
ABCresult
0XX0
10X0
1100
1111
84  
85 # check for any method
86 0 foreach my $name( keys %{$pack} ) {
87 0 my $entry = \$pack->{$name};
88 0 ▶ return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry ne "GLOB" or defined *{$entry;}{"CODE"} 0 0 0
Condition0%0/3
ref $entry ne "GLOB" or defined *{$entry;}{"CODE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $entry ne "GLOB" or defined *{$entry;}{"CODE"}
ref $entry ne "GLOB" defined *{$entry;}{"CODE"}
Truth table0%0/3
A or B
A= ref $entry ne "GLOB"B= defined *{$entry;}{"CODE"}
ABresult
1X1
011
000
89 }
90  
91 # fail
92 0 return 0;
93 }
94  
95  
96 # taken from Sub::Identify
97 0 ▶sub get_code_info {
Subroutine get_code_info: not called CC=4 SCAR=30.0
Pod undocumented
98 0 my ($coderef) = @_;
99 0 ▶ ref($coderef) or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $coderef or return 0 0 0
100  
101 0 my $cv = B::svref_2object($coderef);
102 0 ▶ $cv->isa('B::CV') or return;
Branch: 1 branch
BranchTrueFalseTotal
$cv->isa("B::CV") or return 0 0 0
103  
104 0 my $gv = $cv->GV;
105 0 ▶ $gv->isa('B::GV') or return;
Branch: 1 branch
BranchTrueFalseTotal
$gv->isa("B::GV") or return 0 0 0
106  
107 0 return ($gv->STASH->NAME, $gv->NAME);
108 }
109  
110 0 ▶sub get_code_package{
Subroutine get_code_package: not called CC=3 SCAR=24.8
Pod undocumented
111 0 my($coderef) = @_;
112  
113 0 my $cv = B::svref_2object($coderef);
114 0 ▶ $cv->isa('B::CV') or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cv->isa("B::CV") or return "" 0 0 0
115  
116 0 my $gv = $cv->GV;
117 0 ▶ $gv->isa('B::GV') or return '';
Branch: 1 branch
BranchTrueFalseTotal
$gv->isa("B::GV") or return "" 0 0 0
118  
119 0 return $gv->STASH->NAME;
120 }
121  
122 0 ▶sub get_code_ref{
Subroutine get_code_ref: not called CC=1 SCAR=0.2
Pod undocumented
123 0 my($package, $name) = @_;
124 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
125 3 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
126 3 ▶ use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
127 0 return *{$package . '::' . $name}{CODE};
128 }
129  
130 0 ▶sub generate_isa_predicate_for {
Subroutine generate_isa_predicate_for: not called CC=2 SCAR=17.9
Pod undocumented
131 0 my($for_class, $name) = @_;
132  
133 0 ▶ my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa($for_class) 0 0 0
134  
135 0 ▶ if(defined $name){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
136 0 Mousse::Util::install_subroutines(scalar caller, $name => $predicate);
137 0 return;
138 }
139  
140 0 return $predicate;
141 }
142  
143 0 ▶sub generate_can_predicate_for {
Subroutine generate_can_predicate_for: not called CC=2 SCAR=17.9
Pod undocumented
144 0 my($methods_ref, $name) = @_;
145  
146 0 my @methods = @{$methods_ref};
147  
148 0 ▶ my $predicate = sub{
Subroutine __ANON__: not called CC=4 SCAR=30.0
149 0 my($instance) = @_;
150 0 ▶ if(Scalar::Util::blessed($instance)){
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($instance)) 0 0 0
151 0 foreach my $method(@methods){
152 0 ▶ if(!$instance->can($method)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($instance->can($method)) 0 0 0
153 0 return 0;
154 }
155 }
156 0 return 1;
157 }
158 0 return 0;
159 0 };
160  
161 0 ▶ if(defined $name){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
162 0 Mousse::Util::install_subroutines(scalar caller, $name => $predicate);
163 0 return;
164 }
165  
166 0 return $predicate;
167 }
168  
169 package Mousse::Util::TypeConstraints;
170  
171  
172 0 ▶sub Any { 1 }
Subroutine Any: not called CC=1 SCAR=6.9
Pod undocumented
173 0 ▶sub Item { 1 }
Subroutine Item: not called CC=1 SCAR=6.9
Pod undocumented
174  
175 0 ▶sub Bool { !$_[0] || $_[0] eq '1' }
Subroutine Bool: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
not $_[0] or $_[0] eq 1 0 0 0
176 0 ▶sub Undef { !defined($_[0]) }
Subroutine Undef: not called CC=1 SCAR=6.9
Pod undocumented
177 0 ▶sub Defined { defined($_[0]) }
Subroutine Defined: not called CC=1 SCAR=6.9
Pod undocumented
178 0 ▶sub Value { defined($_[0]) && !ref($_[0]) }
Subroutine Value: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] 0 0 0
179 0 ▶sub Num { Scalar::Util::looks_like_number($_[0]) }
Subroutine Num: not called CC=1 SCAR=6.9
Pod undocumented
180 0 ▶sub Str {
Subroutine Str: not called CC=2 SCAR=17.9
Pod undocumented
181 # We need to use a copy here to flatten MAGICs, for instance as in
182 # Str( substr($_, 0, 42) ).
183 0 my($value) = @_;
184 0 ▶ return defined($value) && ref(\$value) eq 'SCALAR';
Condition0%0/3
defined $value && ref \$value eq 'SCALAR'
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value && ref \$value eq 'SCALAR'
defined $value ref \$value eq 'SCALAR'
Truth table0%0/3
A && B
A= defined $valueB= ref \$value eq 'SCALAR'
ABresult
0X0
100
111
185 }
186 0 ▶sub Int {
Subroutine Int: not called CC=2 SCAR=17.9
Pod undocumented
187 # We need to use a copy here to save the original internal SV flags.
188 0 my($value) = @_;
189 0 ▶ return defined($value) && $value =~ /\A -? [0-9]+ \z/xms;
Condition0%0/3
defined $value && $value =~ /\A -? [0-9]+ \z/msx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value && $value =~ /\A -? [0-9]+ \z/msx
defined $value $value =~ /\A -? [0-9]+ \z/msx
Truth table0%0/3
A && B
A= defined $valueB= $value =~ /\A -? [0-9]+ \z/msx
ABresult
0X0
100
111
190 }
191  
192 0 ▶sub Ref { ref($_[0]) }
Subroutine Ref: not called CC=1 SCAR=6.9
Pod undocumented
193 0 ▶sub ScalarRef {
Subroutine ScalarRef: not called CC=2 SCAR=17.9
Pod undocumented
194 0 my($value) = @_;
195 0 ▶ return ref($value) eq 'SCALAR' || ref($value) eq 'REF';
Condition0%0/3
ref $value eq 'SCALAR' || ref $value eq 'REF'
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $value eq 'SCALAR' || ref $value eq 'REF'
ref $value eq 'SCALAR' ref $value eq 'REF'
Truth table0%0/3
A || B
A= ref $value eq 'SCALAR'B= ref $value eq 'REF'
ABresult
1X1
011
000
196 }
197 0 ▶sub ArrayRef { ref($_[0]) eq 'ARRAY' }
Subroutine ArrayRef: not called CC=1 SCAR=6.9
Pod undocumented
198 0 ▶sub HashRef { ref($_[0]) eq 'HASH' }
Subroutine HashRef: not called CC=1 SCAR=6.9
Pod undocumented
199 0 ▶sub CodeRef { ref($_[0]) eq 'CODE' }
Subroutine CodeRef: not called CC=1 SCAR=6.9
Pod undocumented
200 0 ▶sub RegexpRef { ref($_[0]) eq 'Regexp' }
Subroutine RegexpRef: not called CC=1 SCAR=6.9
Pod undocumented
201 0 ▶sub GlobRef { ref($_[0]) eq 'GLOB' }
Subroutine GlobRef: not called CC=1 SCAR=6.9
Pod undocumented
202  
203 0 ▶sub FileHandle {
Subroutine FileHandle: not called CC=3 SCAR=24.8
Pod undocumented
204 0 my($value) = @_;
205 0 ▶ return Scalar::Util::openhandle($value)
Condition0%0/6
&Scalar::Util::openhandle($value) || builtin::blessed($value) && $value->isa('IO::Handle')
l!l&&r!l&&!r
000
builtin::blessed($value) and $value->isa("IO::Handle")
!ll&&!rl&&r
000
MC/DC0%0/3
&Scalar::Util::openhandle($value) || builtin::blessed($value) && $value->isa('IO::Handle')
&Scalar::Util::openhandle($value) builtin::blessed($value) $value->isa("IO::Handle")
Truth table0%0/4
A || B and C
A= &Scalar::Util::openhandle($value)B= builtin::blessed($value)C= $value->isa("IO::Handle")
ABCresult
1XX1
0111
00X0
0100
206 || (Scalar::Util::blessed($value) && $value->isa("IO::Handle"))
207 }
208  
209 0 ▶sub Object { Scalar::Util::blessed($_[0]) && ref($_[0]) ne 'Regexp' }
Subroutine Object: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and ref $_[0] ne "Regexp" 0 0 0
210  
211 0 ▶sub ClassName { Mousse::Util::is_class_loaded($_[0]) }
Subroutine ClassName: not called CC=1 SCAR=6.9
Pod undocumented
212 0 ▶sub RoleName { (Mousse::Util::class_of($_[0]) || return 0)->isa('Mousse::Meta::Role') }
Subroutine RoleName: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
Mousse::Util::class_of($_[0]) || (return 0)
l!l
00
MC/DC0%0/1
Mousse::Util::class_of($_[0]) || (return 0)
Mousse::Util::class_of($_[0])
Truth table0%0/2
A
A= Mousse::Util::class_of($_[0])
Aresult
11
00
213  
214 0 ▶sub _parameterize_ArrayRef_for {
Subroutine _parameterize_ArrayRef_for: not called CC=1 SCAR=6.9
215 0 my($type_parameter) = @_;
216 0 my $check = $type_parameter->_compiled_type_constraint;
217  
218 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
219 0 foreach my $value (@{$_}) {
220 0 ▶ return undef unless $check->($value);
Branch: 1 branch
BranchTrueFalseTotal
unless &$check($value) 0 0 0
221 }
222 0 return 1;
223 }
224 0 }
225  
226 0 ▶sub _parameterize_HashRef_for {
Subroutine _parameterize_HashRef_for: not called CC=1 SCAR=6.9
227 0 my($type_parameter) = @_;
228 0 my $check = $type_parameter->_compiled_type_constraint;
229  
230 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
231 0 foreach my $value(values %{$_}){
232 0 ▶ return undef unless $check->($value);
Branch: 1 branch
BranchTrueFalseTotal
unless &$check($value) 0 0 0
233 }
234 0 return 1;
235 0 };
236 }
237  
238 # 'Maybe' type accepts 'Any', so it requires parameters
239 0 ▶sub _parameterize_Maybe_for {
Subroutine _parameterize_Maybe_for: not called CC=1 SCAR=6.9
240 0 my($type_parameter) = @_;
241 0 my $check = $type_parameter->_compiled_type_constraint;
242  
243 0 ▶ return sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
244 0 ▶ return !defined($_) || $check->($_);
Condition0%0/3
!defined($_) || &$check($_)
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($_) || &$check($_)
!defined($_) &$check($_)
Truth table0%0/3
not(A) || B
A= !defined($_)B= &$check($_)
ABresult
1X1
011
000
245 0 };
246 }
247  
248 package Mousse::Meta::Module;
249  
250 0 ▶sub name { $_[0]->{package} }
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
251  
252 0 ▶sub _method_map { $_[0]->{methods} }
Subroutine _method_map: not called CC=1 SCAR=6.9
253 0 ▶sub _attribute_map{ $_[0]->{attributes} }
Subroutine _attribute_map: not called CC=1 SCAR=6.9
254  
255 0 ▶sub namespace{
Subroutine namespace: not called CC=1 SCAR=1.2
Pod undocumented
256 0 my $name = $_[0]->{package};
257 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
258 0 return \%{ $name . '::' };
259 }
260  
261 0 ▶sub add_method {
Subroutine add_method: not called CC=4 SCAR=30.0
Pod undocumented
262 0 my($self, $name, $code) = @_;
263  
264 0 ▶ if(!defined $name){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
265 0 $self->throw_error('You must pass a defined name');
266 }
267 0 ▶ if(!defined $code){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code) 0 0 0
268 0 $self->throw_error('You must pass a defined code');
269 }
270  
271 0 ▶ if(ref($code) ne 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code ne "CODE") 0 0 0
272 0 $code = \&{$code}; # coerce
273 }
274  
275 0 $self->{methods}->{$name} = $code; # Moose stores meta object here.
276  
277 0 Mousse::Util::install_subroutines($self->name,
278 $name => $code,
279 );
280 0 return;
281 }
282  
283 my $generate_class_accessor = sub {
284 my($name) = @_;
285 return sub {
286 my $self = shift;
287 if(@_) {
288 return $self->{$name} = shift;
289 }
290  
291 foreach my $class($self->linearized_isa) {
292 my $meta = Mousse::Util::get_metaclass_by_name($class)
293 or next;
294  
295 if(exists $meta->{$name}) {
296 return $meta->{$name};
297 }
298 }
299 return undef;
300 };
301 };
302  
303  
304 package Mousse::Meta::Class;
305  
306 3 ▶use Mousse::Meta::Method::Constructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
307 3 ▶use Mousse::Meta::Method::Destructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
308  
309 0 ▶sub method_metaclass { $_[0]->{method_metaclass} || 'Mousse::Meta::Method' }
Subroutine method_metaclass: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"method_metaclass"} or "Mousse::Meta::Method" 0 0 0
310 0 ▶sub attribute_metaclass { $_[0]->{attribute_metaclass} || 'Mousse::Meta::Attribute' }
Subroutine attribute_metaclass: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"attribute_metaclass"} or "Mousse::Meta::Attribute" 0 0 0
311  
312 0 ▶sub constructor_class { $_[0]->{constructor_class} || 'Mousse::Meta::Method::Constructor' }
Subroutine constructor_class: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"constructor_class"} or "Mousse::Meta::Method::Constructor" 0 0 0
313 0 ▶sub destructor_class { $_[0]->{destructor_class} || 'Mousse::Meta::Method::Destructor' }
Subroutine destructor_class: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"destructor_class"} or "Mousse::Meta::Method::Destructor" 0 0 0
314  
315 0 ▶sub is_anon_class{
Subroutine is_anon_class: not called CC=1 SCAR=6.9
Pod undocumented
316 0 return exists $_[0]->{anon_serial_id};
317 }
318  
319 0 ▶sub roles { $_[0]->{roles} }
Subroutine roles: not called CC=1 SCAR=6.9
Pod undocumented
320  
321 0 ▶sub linearized_isa { @{ Mousse::Util::get_linear_isa($_[0]->{package}) } }
Subroutine linearized_isa: not called CC=1 SCAR=6.9
Pod undocumented
322  
323 0 ▶sub new_object {
Subroutine new_object: not called CC=5 SCAR=34.0
Pod undocumented
324 0 my $meta = shift;
325 0 ▶ my %args = (@_ == 1 ? %{$_[0]} : @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
326  
327 0 my $object = bless {}, $meta->name;
328  
329 0 $meta->_initialize_object($object, \%args, 0);
330 # BUILDALL
331 0 ▶ if( $object->can('BUILD') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("BUILD")) 0 0 0
332 0 for my $class (reverse $meta->linearized_isa) {
333 0 ▶ my $build = Mousse::Util::get_code_ref($class, 'BUILD')
Condition0%0/2
Mousse::Util::get_code_ref($class, 'BUILD') || (next)
l!l
00
MC/DC0%0/1
Mousse::Util::get_code_ref($class, 'BUILD') || (next)
Mousse::Util::get_code_ref($class, 'BUILD')
Truth table0%0/2
A
A= Mousse::Util::get_code_ref($class, 'BUILD')
Aresult
11
00
334 || next;
335  
336 0 $object->$build(\%args);
337 }
338 }
339 0 return $object;
340 }
341  
342 0 ▶sub clone_object {
Subroutine clone_object: not called CC=3 SCAR=24.8
Pod undocumented
343 0 my $class = shift;
344 0 my $object = shift;
345 0 my $args = $object->Mousse::Object::BUILDARGS(@_);
346  
347 0 ▶ (Scalar::Util::blessed($object) && $object->isa($class->name))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($object) and $object->isa($class->name) or $class->throw_error('You must pass an instance of the metaclass (' . $class->name . "), not ($object)") 0 0 0
Condition0%0/3
builtin::blessed($object) and $object->isa($class->name)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($object) and $object->isa($class->name)
builtin::blessed($object) $object->isa($class->name)
Truth table0%0/3
A and B
A= builtin::blessed($object)B= $object->isa($class->name)
ABresult
0X0
100
111
348 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
349  
350 0 my $cloned = bless { %$object }, ref $object;
351 0 $class->_initialize_object($cloned, $args, 1);
352 0 return $cloned;
353 }
354  
355 0 ▶sub _initialize_object{
Subroutine _initialize_object: not called CC=2 SCAR=17.9
356 0 my($self, $object, $args, $is_cloning) = @_;
357 # The initializer, which is used everywhere, must be clear
358 # when an attribute is added. See Mousse::Meta::Class::add_attribute.
359 my $initializer = $self->{_mouse_cache}{_initialize_object} ||=
360 0 ▶ Mousse::Util::load_class($self->constructor_class)
Condition0%0/3
$self->{"_mouse_cache"}{"_initialize_object"} ||= Mousse::Util::load_class($self->constructor_class)->_generate_initialize_object($self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_mouse_cache"}{"_initialize_object"} ||= Mousse::Util::load_class($self->constructor_class)->_generate_initialize_object($self)
$self->{"_mouse_cache"}{"_initialize_object"} Mousse::Util::load_class($self->constructor_class)->_generate_initialize_object($self)
Truth table0%0/3
A ||= B
A= $self->{"_mouse_cache"}{"_initialize_object"}B= Mousse::Util::load_class($self->constructor_class)->_generate_initialize_object($self)
ABresult
1X1
011
000
361 ->_generate_initialize_object($self);
362 0 goto &{$initializer};
363 }
364  
365 0 ▶sub get_all_attributes {
Subroutine get_all_attributes: not called CC=2 SCAR=17.9
Pod undocumented
366 0 my($self) = @_;
367 0 return @{ $self->{_mouse_cache}{all_attributes}
368 0 ▶ ||= $self->_calculate_all_attributes };
Condition0%0/3
$self->{"_mouse_cache"}{"all_attributes"} ||= $self->_calculate_all_attributes
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_mouse_cache"}{"all_attributes"} ||= $self->_calculate_all_attributes
$self->{"_mouse_cache"}{"all_attributes"} $self->_calculate_all_attributes
Truth table0%0/3
A ||= B
A= $self->{"_mouse_cache"}{"all_attributes"}B= $self->_calculate_all_attributes
ABresult
1X1
011
000
369 }
370  
371 0 ▶sub is_immutable { $_[0]->{is_immutable} }
Subroutine is_immutable: not called CC=1 SCAR=6.9
Pod undocumented
372  
373 sub strict_constructor;
374 *strict_constructor = $generate_class_accessor->('strict_constructor');
375  
376 0 ▶sub _invalidate_metaclass_cache {
Subroutine _invalidate_metaclass_cache: not called CC=1 SCAR=6.9
377 0 my($self) = @_;
378 0 delete $self->{_mouse_cache};
379 0 return;
380 }
381  
382 0 ▶sub _report_unknown_args {
Subroutine _report_unknown_args: not called CC=5 SCAR=34.0
383 0 my($metaclass, $attrs, $args) = @_;
384  
385 0 my @unknowns;
386 my %init_args;
387 0 foreach my $attr(@{$attrs}){
388 0 my $init_arg = $attr->init_arg;
389 0 ▶ if(defined $init_arg){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $init_arg) 0 0 0
390 0 $init_args{$init_arg}++;
391 }
392 }
393  
394 0 ▶ while(my $key = each %{$args}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = each %{$args;})) 0 0 0
395 0 ▶ if(!exists $init_args{$key}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $init_args{$key}) 0 0 0
396 0 push @unknowns, $key;
397 }
398 }
399  
400 0 $metaclass->throw_error( sprintf
401 "Unknown attribute passed to the constructor of %s: %s",
402 $metaclass->name, Mousse::Util::english_list(@unknowns),
403 );
404 }
405  
406 package Mousse::Meta::Role;
407  
408 0 ▶sub method_metaclass{ $_[0]->{method_metaclass} || 'Mousse::Meta::Role::Method' }
Subroutine method_metaclass: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"method_metaclass"} or "Mousse::Meta::Role::Method" 0 0 0
409  
410 0 ▶sub is_anon_role{
Subroutine is_anon_role: not called CC=1 SCAR=6.9
Pod undocumented
411 0 return exists $_[0]->{anon_serial_id};
412 }
413  
414 0 ▶sub get_roles { $_[0]->{roles} }
Subroutine get_roles: not called CC=1 SCAR=6.9
Pod undocumented
415  
416 0 ▶sub add_before_method_modifier {
Subroutine add_before_method_modifier: not called CC=2 SCAR=17.9
Pod undocumented
417 0 my ($self, $method_name, $method) = @_;
418  
419 0 ▶ push @{ $self->{before_method_modifiers}{$method_name} ||= [] }, $method;
Condition0%0/2
$self->{"before_method_modifiers"}{$method_name} ||= []
l!l
00
MC/DC0%0/1
$self->{"before_method_modifiers"}{$method_name} ||= []
$self->{"before_method_modifiers"}{$method_name}
Truth table0%0/2
A
A= $self->{"before_method_modifiers"}{$method_name}
Aresult
11
00
420 0 return;
421 }
422 0 ▶sub add_around_method_modifier {
Subroutine add_around_method_modifier: not called CC=2 SCAR=17.9
Pod undocumented
423 0 my ($self, $method_name, $method) = @_;
424  
425 0 ▶ push @{ $self->{around_method_modifiers}{$method_name} ||= [] }, $method;
Condition0%0/2
$self->{"around_method_modifiers"}{$method_name} ||= []
l!l
00
MC/DC0%0/1
$self->{"around_method_modifiers"}{$method_name} ||= []
$self->{"around_method_modifiers"}{$method_name}
Truth table0%0/2
A
A= $self->{"around_method_modifiers"}{$method_name}
Aresult
11
00
426 0 return;
427 }
428 0 ▶sub add_after_method_modifier {
Subroutine add_after_method_modifier: not called CC=2 SCAR=17.9
Pod undocumented
429 0 my ($self, $method_name, $method) = @_;
430  
431 0 ▶ push @{ $self->{after_method_modifiers}{$method_name} ||= [] }, $method;
Condition0%0/2
$self->{"after_method_modifiers"}{$method_name} ||= []
l!l
00
MC/DC0%0/1
$self->{"after_method_modifiers"}{$method_name} ||= []
$self->{"after_method_modifiers"}{$method_name}
Truth table0%0/2
A
A= $self->{"after_method_modifiers"}{$method_name}
Aresult
11
00
432 0 return;
433 }
434  
435 0 ▶sub get_before_method_modifiers {
Subroutine get_before_method_modifiers: not called CC=2 SCAR=17.9
Pod undocumented
436 0 my ($self, $method_name) = @_;
437 0 ▶ return @{ $self->{before_method_modifiers}{$method_name} ||= [] }
Condition0%0/2
$self->{"before_method_modifiers"}{$method_name} ||= []
l!l
00
MC/DC0%0/1
$self->{"before_method_modifiers"}{$method_name} ||= []
$self->{"before_method_modifiers"}{$method_name}
Truth table0%0/2
A
A= $self->{"before_method_modifiers"}{$method_name}
Aresult
11
00
438 }
439 0 ▶sub get_around_method_modifiers {
Subroutine get_around_method_modifiers: not called CC=2 SCAR=17.9
Pod undocumented
440 0 my ($self, $method_name) = @_;
441 0 ▶ return @{ $self->{around_method_modifiers}{$method_name} ||= [] }
Condition0%0/2
$self->{"around_method_modifiers"}{$method_name} ||= []
l!l
00
MC/DC0%0/1
$self->{"around_method_modifiers"}{$method_name} ||= []
$self->{"around_method_modifiers"}{$method_name}
Truth table0%0/2
A
A= $self->{"around_method_modifiers"}{$method_name}
Aresult
11
00
442 }
443 0 ▶sub get_after_method_modifiers {
Subroutine get_after_method_modifiers: not called CC=2 SCAR=17.9
Pod undocumented
444 0 my ($self, $method_name) = @_;
445 0 ▶ return @{ $self->{after_method_modifiers}{$method_name} ||= [] }
Condition0%0/2
$self->{"after_method_modifiers"}{$method_name} ||= []
l!l
00
MC/DC0%0/1
$self->{"after_method_modifiers"}{$method_name} ||= []
$self->{"after_method_modifiers"}{$method_name}
Truth table0%0/2
A
A= $self->{"after_method_modifiers"}{$method_name}
Aresult
11
00
446 }
447  
448 0 ▶sub add_metaclass_accessor { # for meta roles (a.k.a. traits)
Subroutine add_metaclass_accessor: not called CC=1 SCAR=6.9
Pod undocumented
449 0 my($meta, $name) = @_;
450 0 $meta->add_method($name => $generate_class_accessor->($name));
451 0 return;
452 }
453  
454 package Mousse::Meta::Attribute;
455  
456 require Mousse::Meta::Method::Accessor;
457  
458 0 ▶sub accessor_metaclass{ $_[0]->{accessor_metaclass} || 'Mousse::Meta::Method::Accessor' }
Subroutine accessor_metaclass: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"accessor_metaclass"} or "Mousse::Meta::Method::Accessor" 0 0 0
459  
460 # readers
461  
462 0 ▶sub name { $_[0]->{name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
463 0 ▶sub associated_class { $_[0]->{associated_class} }
Subroutine associated_class: not called CC=1 SCAR=6.9
Pod undocumented
464  
465 0 ▶sub accessor { $_[0]->{accessor} }
Subroutine accessor: not called CC=1 SCAR=6.9
Pod undocumented
466 0 ▶sub reader { $_[0]->{reader} }
Subroutine reader: not called CC=1 SCAR=6.9
Pod undocumented
467 0 ▶sub writer { $_[0]->{writer} }
Subroutine writer: not called CC=1 SCAR=6.9
Pod undocumented
468 0 ▶sub predicate { $_[0]->{predicate} }
Subroutine predicate: not called CC=1 SCAR=6.9
Pod undocumented
469 0 ▶sub clearer { $_[0]->{clearer} }
Subroutine clearer: not called CC=1 SCAR=6.9
Pod undocumented
470 0 ▶sub handles { $_[0]->{handles} }
Subroutine handles: not called CC=1 SCAR=6.9
Pod undocumented
471  
472 0 ▶sub _is_metadata { $_[0]->{is} }
Subroutine _is_metadata: not called CC=1 SCAR=6.9
473 0 ▶sub is_required { $_[0]->{required} }
Subroutine is_required: not called CC=1 SCAR=6.9
Pod undocumented
474 0 ▶sub default { $_[0]->{default} }
Subroutine default: not called CC=1 SCAR=6.9
Pod undocumented
475 0 ▶sub is_lazy { $_[0]->{lazy} }
Subroutine is_lazy: not called CC=1 SCAR=6.9
Pod undocumented
476 0 ▶sub is_lazy_build { $_[0]->{lazy_build} }
Subroutine is_lazy_build: not called CC=1 SCAR=6.9
Pod undocumented
477 0 ▶sub is_weak_ref { $_[0]->{weak_ref} }
Subroutine is_weak_ref: not called CC=1 SCAR=6.9
Pod undocumented
478 0 ▶sub init_arg { $_[0]->{init_arg} }
Subroutine init_arg: not called CC=1 SCAR=6.9
Pod undocumented
479 0 ▶sub type_constraint { $_[0]->{type_constraint} }
Subroutine type_constraint: not called CC=1 SCAR=6.9
Pod undocumented
480  
481 0 ▶sub trigger { $_[0]->{trigger} }
Subroutine trigger: not called CC=1 SCAR=6.9
Pod undocumented
482 0 ▶sub builder { $_[0]->{builder} }
Subroutine builder: not called CC=1 SCAR=6.9
Pod undocumented
483 0 ▶sub should_auto_deref { $_[0]->{auto_deref} }
Subroutine should_auto_deref: not called CC=1 SCAR=6.9
Pod undocumented
484 0 ▶sub should_coerce { $_[0]->{coerce} }
Subroutine should_coerce: not called CC=1 SCAR=6.9
Pod undocumented
485  
486 0 ▶sub documentation { $_[0]->{documentation} }
Subroutine documentation: not called CC=1 SCAR=6.9
Pod undocumented
487 0 ▶sub insertion_order { $_[0]->{insertion_order} }
Subroutine insertion_order: not called CC=1 SCAR=6.9
Pod undocumented
488  
489 # predicates
490  
491 0 ▶sub has_accessor { exists $_[0]->{accessor} }
Subroutine has_accessor: not called CC=1 SCAR=6.9
Pod undocumented
492 0 ▶sub has_reader { exists $_[0]->{reader} }
Subroutine has_reader: not called CC=1 SCAR=6.9
Pod undocumented
493 0 ▶sub has_writer { exists $_[0]->{writer} }
Subroutine has_writer: not called CC=1 SCAR=6.9
Pod undocumented
494 0 ▶sub has_predicate { exists $_[0]->{predicate} }
Subroutine has_predicate: not called CC=1 SCAR=6.9
Pod undocumented
495 0 ▶sub has_clearer { exists $_[0]->{clearer} }
Subroutine has_clearer: not called CC=1 SCAR=6.9
Pod undocumented
496 0 ▶sub has_handles { exists $_[0]->{handles} }
Subroutine has_handles: not called CC=1 SCAR=6.9
Pod undocumented
497  
498 0 ▶sub has_default { exists $_[0]->{default} }
Subroutine has_default: not called CC=1 SCAR=6.9
Pod undocumented
499 0 ▶sub has_type_constraint { exists $_[0]->{type_constraint} }
Subroutine has_type_constraint: not called CC=1 SCAR=6.9
Pod undocumented
500 0 ▶sub has_trigger { exists $_[0]->{trigger} }
Subroutine has_trigger: not called CC=1 SCAR=6.9
Pod undocumented
501 0 ▶sub has_builder { exists $_[0]->{builder} }
Subroutine has_builder: not called CC=1 SCAR=6.9
Pod undocumented
502  
503 0 ▶sub has_documentation { exists $_[0]->{documentation} }
Subroutine has_documentation: not called CC=1 SCAR=6.9
Pod undocumented
504  
505 0 ▶sub _process_options{
Subroutine _process_options: not called CC=43 SCAR=75.5
506 0 my($class, $name, $args) = @_;
507  
508 # taken from Class::MOP::Attribute::new
509  
510 0 ▶ defined($name)
Branch: 1 branch
BranchTrueFalseTotal
defined $name or $class->throw_error("You must provide a name for the attribute") 0 0 0
511 or $class->throw_error('You must provide a name for the attribute');
512  
513 0 ▶ if(!exists $args->{init_arg}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"init_arg"}) 0 0 0
514 0 $args->{init_arg} = $name;
515 }
516  
517 # 'required' requires eigher 'init_arg', 'builder', or 'default'
518 0 my $can_be_required = defined( $args->{init_arg} );
519  
520 0 ▶ if(exists $args->{builder}){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args->{'builder'}) { } 0 0 0
elsif (exists $args->{'default'}) { } 0 0 0
521 # XXX:
522 # Moose refuses a CODE ref builder, but Mousse doesn't for backward compatibility
523 # This feature will be changed in a future. (gfx)
524 $class->throw_error('builder must be a defined scalar value which is a method name')
525 #if ref $args->{builder} || !defined $args->{builder};
526 0 ▶ if !defined $args->{builder};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"builder"} 0 0 0
527  
528 0 $can_be_required++;
529 }
530 elsif(exists $args->{default}){
531 0 ▶ if(ref $args->{default} && ref($args->{default}) ne 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{"default"} and ref $args->{"default"} ne "CODE") 0 0 0
Condition0%0/3
ref $args->{"default"} and ref $args->{"default"} ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $args->{"default"} and ref $args->{"default"} ne "CODE"
ref $args->{"default"} ref $args->{"default"} ne "CODE"
Truth table0%0/3
A and B
A= ref $args->{"default"}B= ref $args->{"default"} ne "CODE"
ABresult
0X0
100
111
532 0 $class->throw_error("References are not allowed as default values, you must "
533 . "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])");
534 }
535 0 $can_be_required++;
536 }
537  
538 0 ▶ if( $args->{required} && !$can_be_required ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"required"} and not $can_be_required) 0 0 0
Condition0%0/3
$args->{"required"} and not $can_be_required
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"required"} and not $can_be_required
$args->{"required"} not $can_be_required
Truth table0%0/3
A and not(B)
A= $args->{"required"}B= not $can_be_required
ABresult
0X0
100
111
539 0 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg");
540 }
541  
542 # taken from Mousse::Meta::Attribute->new and ->_process_args
543  
544 0 ▶ if(exists $args->{is}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"is"}) 0 0 0
545 0 my $is = $args->{is};
546  
547 0 ▶ if($is eq 'ro'){
Branch: 3 branches
BranchTrueFalseTotal
if ($is eq 'ro') { } 0 0 0
elsif ($is eq 'rw') { } 0 0 0
elsif ($is eq 'bare') { } 0 0 0
548 0 ▶ $args->{reader} ||= $name;
Condition0%0/3
$args->{"reader"} ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"reader"} ||= $name
$args->{"reader"} $name
Truth table0%0/3
A ||= B
A= $args->{"reader"}B= $name
ABresult
1X1
011
000
549 }
550 elsif($is eq 'rw'){
551 0 ▶ if(exists $args->{writer}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'writer'}) { } 0 0 0
552 0 ▶ $args->{reader} ||= $name;
Condition0%0/3
$args->{"reader"} ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"reader"} ||= $name
$args->{"reader"} $name
Truth table0%0/3
A ||= B
A= $args->{"reader"}B= $name
ABresult
1X1
011
000
553 }
554 else{
555 0 ▶ $args->{accessor} ||= $name;
Condition0%0/3
$args->{"accessor"} ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"accessor"} ||= $name
$args->{"accessor"} $name
Truth table0%0/3
A ||= B
A= $args->{"accessor"}B= $name
ABresult
1X1
011
000
556 }
557 }
558 elsif($is eq 'bare'){
559 # do nothing, but don't complain (later) about missing methods
560 }
561 else{
562 0 ▶ $is = 'undef' if !defined $is;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $is 0 0 0
563 0 $class->throw_error("I do not understand this option (is => $is) on attribute ($name)");
564 }
565 }
566  
567 0 my $tc;
568 0 ▶ if(exists $args->{isa}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"isa"}) 0 0 0
569 0 $tc = $args->{type_constraint} = Mousse::Util::TypeConstraints::find_or_create_isa_type_constraint($args->{isa});
570 }
571  
572 0 ▶ if(exists $args->{does}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"does"}) 0 0 0
573 0 ▶ if(defined $tc){ # both isa and does supplied
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tc) { } 0 0 0
574 0 my $does_ok = do{
575 0 local $@;
576 0 eval{ "$tc"->does($args->{does}) };
577 };
578 0 ▶ if(!$does_ok){
Branch: 1 branch
BranchTrueFalseTotal
unless ($does_ok) 0 0 0
579 0 $class->throw_error("Cannot have both an isa option and a does option because '$tc' does not do '$args->{does}' on attribute ($name)");
580 }
581 }
582 else {
583 0 $tc = $args->{type_constraint} = Mousse::Util::TypeConstraints::find_or_create_does_type_constraint($args->{does});
584 }
585 }
586  
587 0 ▶ if($args->{coerce}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"coerce"}) 0 0 0
588 0 ▶ defined($tc)
Branch: 1 branch
BranchTrueFalseTotal
defined $tc or $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)") 0 0 0
589 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)");
590  
591 $args->{weak_ref}
592 0 ▶ && $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)");
Branch: 1 branch
BranchTrueFalseTotal
$args->{"weak_ref"} and $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)") 0 0 0
593 }
594  
595 0 ▶ if ($args->{lazy_build}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"lazy_build"}) 0 0 0
596 exists($args->{default})
597 0 ▶ && $class->throw_error("You can not use lazy_build and default for the same attribute ($name)");
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{"default"} and $class->throw_error("You can not use lazy_build and default for the same attribute ($name)") 0 0 0
598  
599 0 $args->{lazy} = 1;
600 0 ▶ $args->{builder} ||= "_build_${name}";
Condition0%0/2
$args->{"builder"} ||= "_build_$name"
l!l
00
MC/DC0%0/1
$args->{"builder"} ||= "_build_$name"
$args->{"builder"}
Truth table0%0/2
A
A= $args->{"builder"}
Aresult
11
00
601 0 ▶ if ($name =~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_/) { } 0 0 0
602 0 ▶ $args->{clearer} ||= "_clear${name}";
Condition0%0/2
$args->{"clearer"} ||= "_clear$name"
l!l
00
MC/DC0%0/1
$args->{"clearer"} ||= "_clear$name"
$args->{"clearer"}
Truth table0%0/2
A
A= $args->{"clearer"}
Aresult
11
00
603 0 ▶ $args->{predicate} ||= "_has${name}";
Condition0%0/2
$args->{"predicate"} ||= "_has$name"
l!l
00
MC/DC0%0/1
$args->{"predicate"} ||= "_has$name"
$args->{"predicate"}
Truth table0%0/2
A
A= $args->{"predicate"}
Aresult
11
00
604 }
605 else {
606 0 ▶ $args->{clearer} ||= "clear_${name}";
Condition0%0/2
$args->{"clearer"} ||= "clear_$name"
l!l
00
MC/DC0%0/1
$args->{"clearer"} ||= "clear_$name"
$args->{"clearer"}
Truth table0%0/2
A
A= $args->{"clearer"}
Aresult
11
00
607 0 ▶ $args->{predicate} ||= "has_${name}";
Condition0%0/2
$args->{"predicate"} ||= "has_$name"
l!l
00
MC/DC0%0/1
$args->{"predicate"} ||= "has_$name"
$args->{"predicate"}
Truth table0%0/2
A
A= $args->{"predicate"}
Aresult
11
00
608 }
609 }
610  
611 0 ▶ if ($args->{auto_deref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"auto_deref"}) 0 0 0
612 0 ▶ defined($tc)
Branch: 1 branch
BranchTrueFalseTotal
defined $tc or $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)") 0 0 0
613 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)");
614  
615 0 ▶ ( $tc->is_a_type_of('ArrayRef') || $tc->is_a_type_of('HashRef') )
Branch: 1 branch
BranchTrueFalseTotal
$tc->is_a_type_of("ArrayRef") or $tc->is_a_type_of("HashRef") or $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)") 0 0 0
Condition0%0/3
$tc->is_a_type_of("ArrayRef") or $tc->is_a_type_of("HashRef")
l!l&&r!l&&!r
000
MC/DC0%0/2
$tc->is_a_type_of("ArrayRef") or $tc->is_a_type_of("HashRef")
$tc->is_a_type_of("ArrayRef") $tc->is_a_type_of("HashRef")
Truth table0%0/3
A or B
A= $tc->is_a_type_of("ArrayRef")B= $tc->is_a_type_of("HashRef")
ABresult
1X1
011
000
616 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)");
617 }
618  
619 0 ▶ if (exists $args->{trigger}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"trigger"}) 0 0 0
620 ('CODE' eq ref $args->{trigger})
621 0 ▶ || $class->throw_error("Trigger must be a CODE ref on attribute ($name)");
Branch: 1 branch
BranchTrueFalseTotal
"CODE" eq ref $args->{"trigger"} or $class->throw_error("Trigger must be a CODE ref on attribute ($name)") 0 0 0
622 }
623  
624 0 ▶ if ($args->{lazy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"lazy"}) 0 0 0
625 (exists $args->{default} || defined $args->{builder})
626 0 ▶ || $class->throw_error("You cannot have a lazy attribute ($name) without specifying a default value for it");
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{"default"} or defined $args->{"builder"} or $class->throw_error("You cannot have a lazy attribute ($name) without specifying a default value for it") 0 0 0
Condition0%0/3
exists $args->{"default"} or defined $args->{"builder"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $args->{"default"} or defined $args->{"builder"}
exists $args->{"default"} defined $args->{"builder"}
Truth table0%0/3
A or B
A= exists $args->{"default"}B= defined $args->{"builder"}
ABresult
1X1
011
000
627 }
628  
629 0 return;
630 }
631  
632  
633 package Mousse::Meta::TypeConstraint;
634  
635 use overload
636 3 ▶ '""' => '_as_string',
Subroutine BEGIN: called CC=1 SCAR=0.0
637 '0+' => '_identity',
638 '|' => '_unite',
639  
640 3 fallback => 1;
641  
642 0 ▶sub name { $_[0]->{name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
643 0 ▶sub parent { $_[0]->{parent} }
Subroutine parent: not called CC=1 SCAR=6.9
Pod undocumented
644 0 ▶sub message { $_[0]->{message} }
Subroutine message: not called CC=1 SCAR=6.9
Pod undocumented
645  
646 0 ▶sub _identity { Scalar::Util::refaddr($_[0]) } # overload 0+
Subroutine _identity: not called CC=1 SCAR=6.9
647  
648 0 ▶sub type_parameter { $_[0]->{type_parameter} }
Subroutine type_parameter: not called CC=1 SCAR=6.9
Pod undocumented
649 0 ▶sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
Subroutine _compiled_type_constraint: not called CC=1 SCAR=6.9
650  
651 0 ▶sub __is_parameterized { exists $_[0]->{type_parameter} }
Subroutine __is_parameterized: not called CC=1 SCAR=6.9
652 0 ▶sub has_coercion { exists $_[0]->{_compiled_type_coercion} }
Subroutine has_coercion: not called CC=1 SCAR=6.9
Pod undocumented
653  
654  
655 0 ▶sub compile_type_constraint{
Subroutine compile_type_constraint: not called CC=7 SCAR=40.3
Pod undocumented
656 0 my($self) = @_;
657  
658 # add parents first
659 0 my @checks;
660 0 ▶ for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) 0 0 0
661 0 ▶ if($parent->{hand_optimized_type_constraint}){
Branch: 2 branches
BranchTrueFalseTotal
if ($parent->{'hand_optimized_type_constraint'}) { } 0 0 0
elsif ($parent->{'constraint'}) { } 0 0 0
662 0 unshift @checks, $parent->{hand_optimized_type_constraint};
663 0 last; # a hand optimized constraint must include all the parents
664 }
665 elsif($parent->{constraint}){
666 0 unshift @checks, $parent->{constraint};
667 }
668 }
669  
670 # then add child
671 0 ▶ if($self->{constraint}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"constraint"}) 0 0 0
672 0 push @checks, $self->{constraint};
673 }
674  
675 0 ▶ if($self->{type_constraints}){ # Union
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type_constraints"}) 0 0 0
676 0 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
677 0 ▶ push @checks, sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
678 0 foreach my $c(@types){
679 0 ▶ return 1 if $c->($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &$c($_[0]) 0 0 0
680 }
681 0 return 0;
682 0 };
683 }
684  
685 0 ▶ if(@checks == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (@checks == 0) { } 0 0 0
686 0 $self->{compiled_type_constraint} = \&Mousse::Util::TypeConstraints::Any;
687 }
688 else{
689 0 ▶ $self->{compiled_type_constraint} = sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
690 0 my(@args) = @_;
691 0 local $_ = $args[0];
692 0 foreach my $c(@checks){
693 0 ▶ return undef if !$c->(@args);
Branch: 1 branch
BranchTrueFalseTotal
unless &$c(@args) 0 0 0
694 }
695 0 return 1;
696 0 };
697 }
698 0 return;
699 }
700  
701 0 ▶sub check {
Subroutine check: not called CC=1 SCAR=6.9
Pod undocumented
702 0 my $self = shift;
703 0 return $self->_compiled_type_constraint->(@_);
704 }
705  
706  
707 package Mousse::Object;
708  
709 0 ▶sub BUILDARGS {
Subroutine BUILDARGS: not called CC=3 SCAR=24.8
Pod undocumented
710 0 my $class = shift;
711  
712 0 ▶ if (scalar @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 0 0
713 0 ▶ (ref($_[0]) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "HASH" or $class->meta->throw_error("Single parameters to new() must be a HASH ref") 0 0 0
714 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
715  
716 0 return {%{$_[0]}};
717 }
718 else {
719 0 return {@_};
720 }
721 }
722  
723 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
724 0 my $class = shift;
725 0 my $args = $class->BUILDARGS(@_);
726 0 return $class->meta->new_object($args);
727 }
728  
729 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=5 SCAR=30.3
730 0 my $self = shift;
731  
732 0 ▶ return unless $self->can('DEMOLISH'); # short circuit
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("DEMOLISH") 0 0 0
733  
734 0 my $e = do{
735 0 local $?;
736 0 local $@;
737 0 eval{
738 # DEMOLISHALL
739  
740 # We cannot count on being able to retrieve a previously made
741 # metaclass, _or_ being able to make a new one during global
742 # destruction. However, we should still be able to use mro at
743 # that time (at least tests suggest so ;)
744  
745 0 foreach my $class (@{ Mousse::Util::get_linear_isa(ref $self) }) {
746 0 ▶ my $demolish = Mousse::Util::get_code_ref($class, 'DEMOLISH')
Condition0%0/2
Mousse::Util::get_code_ref($class, 'DEMOLISH') || (next)
l!l
00
MC/DC0%0/1
Mousse::Util::get_code_ref($class, 'DEMOLISH') || (next)
Mousse::Util::get_code_ref($class, 'DEMOLISH')
Truth table0%0/2
A
A= Mousse::Util::get_code_ref($class, 'DEMOLISH')
Aresult
11
00
747 || next;
748  
749 0 $self->$demolish($Mousse::Util::in_global_destruction);
750 }
751 };
752 0 $@;
753 };
754  
755 3 ▶ no warnings 'misc';
Subroutine BEGIN: called CC=1 SCAR=0.0
756 0 ▶ die $e if $e; # rethrow
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 0 0
757 }
758  
759 0 ▶sub BUILDALL {
Subroutine BUILDALL: not called CC=4 SCAR=30.0
Pod undocumented
760 0 my $self = shift;
761  
762 # short circuit
763 0 ▶ return unless $self->can('BUILD');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("BUILD") 0 0 0
764  
765 0 for my $class (reverse $self->meta->linearized_isa) {
766 0 ▶ my $build = Mousse::Util::get_code_ref($class, 'BUILD')
Condition0%0/2
Mousse::Util::get_code_ref($class, 'BUILD') || (next)
l!l
00
MC/DC0%0/1
Mousse::Util::get_code_ref($class, 'BUILD') || (next)
Mousse::Util::get_code_ref($class, 'BUILD')
Truth table0%0/2
A
A= Mousse::Util::get_code_ref($class, 'BUILD')
Aresult
11
00
767 || next;
768  
769 0 $self->$build(@_);
770 }
771 0 return;
772 }
773  
774 sub DEMOLISHALL;
775 *DEMOLISHALL = \&DESTROY;
776  
777 # Contents of Mouse::Exporter
778 package Mousse::Exporter;
779 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
780 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
781 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
782  
783 my %SPEC;
784  
785 my $strict_bits;
786 my $warnings_extra_bits;
787 3 ▶BEGIN{
Subroutine BEGIN: called CC=1 SCAR=0.0
788 3 $strict_bits = strict::bits(qw(subs refs vars));
789 3 $warnings_extra_bits = warnings::bits(FATAL => 'recursion');
790 }
791  
792 # it must be "require", because Mousse::Util depends on Mousse::Exporter,
793 # which depends on Mousse::Util::import()
794 require Mousse::Util;
795  
796 3 ▶sub import{
Subroutine import: called CC=1 SCAR=0.0
797 # strict->import;
798 3 $^H |= $strict_bits;
799 # warnings->import('all', FATAL => 'recursion');
800 3 ${^WARNING_BITS} |= $warnings::Bits{all};
801 3 ${^WARNING_BITS} |= $warnings_extra_bits;
802 3 return;
803 }
804  
805  
806 3 ▶sub setup_import_methods{
Subroutine setup_import_methods: called CC=2 SCAR=9.2
Pod undocumented
807 3 my($class, %args) = @_;
808  
809 3 ▶ my $exporting_package = $args{exporting_package} ||= caller();
Condition33%1/3
$args{"exporting_package"} ||= caller()
l!l&&r!l&&!r
030
MC/DC0%0/2
$args{"exporting_package"} ||= caller()
$args{"exporting_package"} caller()
Truth table33%1/3
A ||= B
A= $args{"exporting_package"}B= caller()
ABresult
1X1
011
000
810  
811 3 my($import, $unimport) = $class->build_import_methods(%args);
812  
813 Mousse::Util::install_subroutines($exporting_package,
814 import => $import,
815 unimport => $unimport,
816  
817 0 ▶ export_to_level => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
818 0 my($package, $level, undef, @args) = @_; # the third argument is redundant
819 0 $package->import({ into_level => $level + 1 }, @args);
820 },
821 0 ▶ export => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
822 0 my($package, $into, @args) = @_;
823 0 $package->import({ into => $into }, @args);
824 },
825 3 );
826 3 return;
827 }
828  
829 3 ▶sub build_import_methods{
Subroutine build_import_methods: called CC=20 SCAR=40.5
Pod undocumented
830 3 my($self, %args) = @_;
831  
832 3 ▶ my $exporting_package = $args{exporting_package} ||= caller();
Condition33%1/3
$args{"exporting_package"} ||= caller()
l!l&&r!l&&!r
300
MC/DC0%0/2
$args{"exporting_package"} ||= caller()
$args{"exporting_package"} caller()
Truth table33%1/3
A ||= B
A= $args{"exporting_package"}B= caller()
ABresult
1X1
011
000
833  
834 3 $SPEC{$exporting_package} = \%args;
835  
836 # canonicalize args
837 3 my @export_from;
838 3 ▶ if($args{also}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'also'}) { } 0 3 3
839 0 my %seen;
840 0 my @stack = ($exporting_package);
841  
842 0 ▶ while(my $current = shift @stack){
Branch: 1 branch
BranchTrueFalseTotal
if (my $current = shift @stack) 0 0 0
843 0 push @export_from, $current;
844  
845 0 ▶ my $also = $SPEC{$current}{also} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $also = $SPEC{$current}{"also"} or next 0 0 0
846 0 ▶ push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
Branch: 1 branch
BranchTrueFalseTotal
ref $also ? : 0 0 0
847 }
848 }
849 else{
850 3 @export_from = ($exporting_package);
851 }
852  
853 3 my %exports;
854 my @removables;
855 3 my @all;
856  
857 3 my @init_meta_methods;
858  
859 3 foreach my $package(@export_from){
860 3 ▶ my $spec = $SPEC{$package} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $SPEC{$package} or next 3 0 3
861  
862 3 ▶ if(my $as_is = $spec->{as_is}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $as_is = $spec->{"as_is"}) 3 0 3
863 3 foreach my $thingy (@{$as_is}){
864 48 my($code_package, $code_name, $code);
865  
866 48 ▶ if(ref($thingy)){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thingy) { } 0 48 48
867 0 $code = $thingy;
868 0 ($code_package, $code_name) = Mousse::Util::get_code_info($code);
869 }
870 else{
871 48 $code_package = $package;
872 48 $code_name = $thingy;
873 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
874 48 $code = \&{ $code_package . '::' . $code_name };
875 }
876  
877 48 push @all, $code_name;
878 48 $exports{$code_name} = $code;
879 48 ▶ if($code_package eq $package){
Branch: 1 branch
BranchTrueFalseTotal
if ($code_package eq $package) 48 0 48
880 48 push @removables, $code_name;
881 }
882 }
883 }
884  
885 3 ▶ if(my $init_meta = $package->can('init_meta')){
Branch: 1 branch
BranchTrueFalseTotal
if (my $init_meta = $package->can("init_meta")) 0 3 3
886 0 ▶ if(!grep{ $_ == $init_meta } @init_meta_methods){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ == $init_meta;} @init_meta_methods) 0 0 0
887 0 push @init_meta_methods, $init_meta;
888 }
889 }
890 }
891 3 $args{EXPORTS} = \%exports;
892 3 $args{REMOVABLES} = \@removables;
893  
894 3 ▶ $args{groups}{all} ||= \@all;
Condition50%1/2
$args{"groups"}{"all"} ||= \@all
l!l
03
MC/DC0%0/1
$args{"groups"}{"all"} ||= \@all
$args{"groups"}{"all"}
Truth table50%1/2
A
A= $args{"groups"}{"all"}
Aresult
11
00
895  
896 3 ▶ if(my $default_list = $args{groups}{default}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $default_list = $args{'groups'}{'default'}) { } 3 0 3
897 3 my %default;
898 3 foreach my $keyword(@{$default_list}){
899 0 ▶ $default{$keyword} = $exports{$keyword}
Condition0%0/2
$exports{$keyword} || Carp::confess(qq[The $exporting_package package does not export "$keyword"])
l!l
00
MC/DC0%0/1
$exports{$keyword} || Carp::confess(qq[The $exporting_package package does not export "$keyword"])
$exports{$keyword}
Truth table0%0/2
A
A= $exports{$keyword}
Aresult
11
00
900 || Carp::confess(qq{The $exporting_package package does not export "$keyword"});
901 }
902 3 $args{DEFAULT} = \%default;
903 }
904 else{
905 0 ▶ $args{groups}{default} ||= \@all;
Condition0%0/2
$args{"groups"}{"default"} ||= \@all
l!l
00
MC/DC0%0/1
$args{"groups"}{"default"} ||= \@all
$args{"groups"}{"default"}
Truth table0%0/2
A
A= $args{"groups"}{"default"}
Aresult
11
00
906 0 $args{DEFAULT} = $args{EXPORTS};
907 }
908  
909 3 ▶ if(@init_meta_methods){
Branch: 1 branch
BranchTrueFalseTotal
if (@init_meta_methods) 0 3 3
910 0 $args{INIT_META} = \@init_meta_methods;
911 }
912  
913 3 return (\&do_import, \&do_unimport);
914 }
915  
916 # the entity of general import()
917 0 ▶sub do_import {
Subroutine do_import: not called CC=18 SCAR=58.3
Pod undocumented
918 0 my($package, @args) = @_;
919  
920 0 ▶ my $spec = $SPEC{$package}
Condition0%0/2
$SPEC{$package} || Carp::confess("The package $package package does not use Mousse::Exporter")
l!l
00
MC/DC0%0/1
$SPEC{$package} || Carp::confess("The package $package package does not use Mousse::Exporter")
$SPEC{$package}
Truth table0%0/2
A
A= $SPEC{$package}
Aresult
11
00
921 || Carp::confess("The package $package package does not use Mousse::Exporter");
922  
923 0 ▶ my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 0 0
924  
925 0 my @exports;
926 my @traits;
927  
928 0 ▶ while(@args){
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
929 0 my $arg = shift @args;
930 0 ▶ if($arg =~ s/^-//){
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ s/^-//) { } 0 0 0
elsif ($arg =~ s/^://) { } 0 0 0
931 0 ▶ if($arg eq 'traits'){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq 'traits') { } 0 0 0
932 0 ▶ push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 0 0
933 }
934 else {
935 0 Mousse::Util::not_supported("-$arg");
936 }
937 }
938 elsif($arg =~ s/^://){
939 0 ▶ my $group = $spec->{groups}{$arg}
Condition0%0/2
$spec->{'groups'}{$arg} || Carp::confess(qq[The $package package does not export the group "$arg"])
l!l
00
MC/DC0%0/1
$spec->{'groups'}{$arg} || Carp::confess(qq[The $package package does not export the group "$arg"])
$spec->{'groups'}{$arg}
Truth table0%0/2
A
A= $spec->{'groups'}{$arg}
Aresult
11
00
940 || Carp::confess(qq{The $package package does not export the group "$arg"});
941 0 push @exports, @{$group};
942 }
943 else{
944 0 push @exports, $arg;
945 }
946 }
947  
948 # strict->import;
949 0 $^H |= $strict_bits;
950 # warnings->import('all', FATAL => 'recursion');
951 0 ${^WARNING_BITS} |= $warnings::Bits{all};
952 0 ${^WARNING_BITS} |= $warnings_extra_bits;
953  
954 0 ▶ if($spec->{INIT_META}){
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->{'INIT_META'}) { } 0 0 0
elsif (@traits) { } 0 0 0
955 0 my $meta;
956 0 foreach my $init_meta(@{$spec->{INIT_META}}){
957 0 $meta = $package->$init_meta(for_class => $into);
958 }
959  
960 0 ▶ if(@traits){
Branch: 1 branch
BranchTrueFalseTotal
if (@traits) 0 0 0
961 0 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
962 @traits = map{
963 0 ▶ ref($_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
964 ? $_
965 : Mousse::Util::resolve_metaclass_alias($type => $_, trait => 1)
966 } @traits;
967  
968 0 require Mousse::Util::MetaRole;
969 0 ▶ Mousse::Util::MetaRole::apply_metaroles(
Branch: 1 branch
BranchTrueFalseTotal
Mousse::Util::is_a_metarole($into->meta) ? : 0 0 0
970 for => $into,
971 Mousse::Util::is_a_metarole($into->meta)
972 ? (role_metaroles => { role => \@traits })
973 : (class_metaroles => { class => \@traits }),
974 );
975 }
976 }
977 elsif(@traits){
978 0 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
979 }
980  
981 0 ▶ if(@exports){
Branch: 1 branch
BranchTrueFalseTotal
if (@exports) { } 0 0 0
982 0 my @export_table;
983 0 foreach my $keyword(@exports){
984 push @export_table,
985 0 ▶ $keyword => ($spec->{EXPORTS}{$keyword}
Condition0%0/2
$spec->{'EXPORTS'}{$keyword} || Carp::confess(qq[The $package package does not export "$keyword"])
l!l
00
MC/DC0%0/1
$spec->{'EXPORTS'}{$keyword} || Carp::confess(qq[The $package package does not export "$keyword"])
$spec->{'EXPORTS'}{$keyword}
Truth table0%0/2
A
A= $spec->{'EXPORTS'}{$keyword}
Aresult
11
00
986 || Carp::confess(qq{The $package package does not export "$keyword"})
987 );
988 }
989 0 Mousse::Util::install_subroutines($into, @export_table);
990 }
991 else{
992 0 Mousse::Util::install_subroutines($into, %{$spec->{DEFAULT}});
993 }
994 0 return;
995 }
996  
997 # the entity of general unimport()
998 0 ▶sub do_unimport {
Subroutine do_unimport: not called CC=6 SCAR=34.3
Pod undocumented
999 0 my($package, $arg) = @_;
1000  
1001 0 ▶ my $spec = $SPEC{$package}
Condition0%0/2
$SPEC{$package} || Carp::confess("The package $package does not use Mousse::Exporter")
l!l
00
MC/DC0%0/1
$SPEC{$package} || Carp::confess("The package $package does not use Mousse::Exporter")
$SPEC{$package}
Truth table0%0/2
A
A= $SPEC{$package}
Aresult
11
00
1002 || Carp::confess("The package $package does not use Mousse::Exporter");
1003  
1004 0 my $from = _get_caller_package($arg);
1005  
1006 0 my $stash = do{
1007 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1008 0 \%{$from . '::'}
1009 };
1010  
1011 0 for my $keyword (@{ $spec->{REMOVABLES} }) {
1012 0 ▶ next if !exists $stash->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$keyword} 0 0 0
1013 0 my $gv = \$stash->{$keyword};
1014  
1015 # remove what is from us
1016 0 ▶ if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $gv eq "GLOB" and *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}) 0 0 0
Condition0%0/3
ref $gv eq "GLOB" and *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $gv eq "GLOB" and *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}
ref $gv eq "GLOB" *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}
Truth table0%0/3
A and B
A= ref $gv eq "GLOB"B= *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}
ABresult
0X0
100
111
1017 0 delete $stash->{$keyword};
1018 }
1019 }
1020 0 return;
1021 }
1022  
1023 0 ▶sub _get_caller_package {
Subroutine _get_caller_package: not called CC=4 SCAR=30.0
1024 0 my($arg) = @_;
1025  
1026 # We need one extra level because it's called by import so there's a layer
1027 # of indirection
1028 0 ▶ if(ref $arg){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 0 0 0
1029 return defined($arg->{into}) ? $arg->{into}
1030 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
1031 0 ▶ : scalar caller(1);
Branch: 2 branches
BranchTrueFalseTotal
defined $arg->{'into'} ? : 0 0 0
defined $arg->{'into_level'} ? : 0 0 0
1032 }
1033 else{
1034 0 return scalar caller(1);
1035 }
1036 }
1037  
1038 # Contents of Mouse::Util
1039 package Mousse::Util;
1040 3 ▶use Mousse::Exporter; # enables strict and warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
1041 3 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
1042  
1043 # Note that those which don't exist here are defined in XS or Mousse::PurePerl
1044  
1045 # must be here because it will be refered by other modules loaded
1046 sub get_linear_isa($;$); ## no critic
1047  
1048 # must be here because it will called in Mousse::Exporter
1049 3 ▶sub install_subroutines {
Subroutine install_subroutines: called CC=2 SCAR=6.9
Pod undocumented
1050 3 my $into = shift;
1051  
1052 3 ▶ while(my($name, $code) = splice @_, 0, 2){
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 12 3 15
1053 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1054 3 ▶ no warnings 'once', 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
1055 3 ▶ use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1056 12 *{$into . '::' . $name} = \&{$code};
1057 }
1058 3 return;
1059 }
1060  
1061 3 ▶BEGIN{
Subroutine BEGIN: called CC=9 SCAR=39.1
1062 # This is used in Mousse::PurePerl
1063 3 Mousse::Exporter->setup_import_methods(
1064 as_is => [qw(
1065 find_meta
1066 does_role
1067 resolve_metaclass_alias
1068 apply_all_roles
1069 english_list
1070  
1071 load_class
1072 is_class_loaded
1073  
1074 get_linear_isa
1075 get_code_info
1076  
1077 get_code_package
1078 get_code_ref
1079  
1080 not_supported
1081  
1082 does meta throw_error dump
1083 )],
1084 groups => {
1085 default => [], # export no functions by default
1086  
1087 # The ':meta' group is 'use metaclass' for Mousse
1088 meta => [qw(does meta dump throw_error)],
1089 },
1090 );
1091  
1092 3 our $VERSION = '0.93';
1093  
1094 3 ▶ my $xs = !(defined(&is_valid_class_name) || $ENV{MOUSE_PUREPERL} || $ENV{PERL_ONLY});
Condition33%2/6
defined &Mousse::Util::is_valid_class_name || $ENV{'MOUSE_PUREPERL'} || $ENV{'PERL_ONLY'}
l!l&&r!l&&!r
300
defined &Mousse::Util::is_valid_class_name or $ENV{"MOUSE_PUREPERL"}
l!l&&r!l&&!r
300
MC/DC0%0/3
defined &Mousse::Util::is_valid_class_name || $ENV{'MOUSE_PUREPERL'} || $ENV{'PERL_ONLY'}
defined &Mousse::Util::is_valid_class_name $ENV{"MOUSE_PUREPERL"} $ENV{'PERL_ONLY'}
Truth table25%1/4
A or B || C
A= defined &Mousse::Util::is_valid_class_nameB= $ENV{"MOUSE_PUREPERL"}C= $ENV{'PERL_ONLY'}
ABCresult
1XX1
01X1
0011
0000
1095  
1096 # Because Mousse::Util is loaded first in all the Mousse sub-modules,
1097 # XSLoader must be placed here, not in Mousse.pm.
1098 3 ▶ if($xs){
Branch: 1 branch
BranchTrueFalseTotal
if ($xs) 0 3 3
1099 # XXX: XSLoader tries to get the object path from caller's file name
1100 # $hack_mouse_file fools its mechanism
1101 0 (my $hack_mouse_file = __FILE__) =~ s/.Util//; # .../Mousse/Util.pm -> .../Mousse.pm
1102 0 ▶ $xs = eval sprintf("#line %d %s\n", __LINE__, $hack_mouse_file) . q{
Condition0%0/2
eval sprintf("#line %d %s\n", __LINE__, $hack_mouse_file) . "\n\t\t\tlocal \$^W = 0; # workaround 'redefine' warning to &install_subroutines\n\t\t\trequire XSLoader;\n\t\t\tXSLoader::load('Mousse', \$VERSION);\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Constructor::XS' }, ':meta');\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Destructor::XS' }, ':meta');\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Accessor::XS' }, ':meta');\n\t\t\treturn 1;\n\t\t" || 0
l!l
00
MC/DC0%0/1
eval sprintf("#line %d %s\n", __LINE__, $hack_mouse_file) . "\n\t\t\tlocal \$^W = 0; # workaround 'redefine' warning to &install_subroutines\n\t\t\trequire XSLoader;\n\t\t\tXSLoader::load('Mousse', \$VERSION);\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Constructor::XS' }, ':meta');\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Destructor::XS' }, ':meta');\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Accessor::XS' }, ':meta');\n\t\t\treturn 1;\n\t\t" || 0
eval sprintf("#line %d %s\n", __LINE__, $hack_mouse_file) . "\n\t\t\tlocal \$^W = 0; # workaround 'redefine' warning to &install_subroutines\n\t\t\trequire XSLoader;\n\t\t\tXSLoader::load('Mousse', \$VERSION);\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Constructor::XS' }, ':meta');\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Destructor::XS' }, ':meta');\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Accessor::XS' }, ':meta');\n\t\t\treturn 1;\n\t\t"
Truth table0%0/2
A
A= eval sprintf("#line %d %s\n", __LINE__, $hack_mouse_file) . "\n\t\t\tlocal \$^W = 0; # workaround 'redefine' warning to &install_subroutines\n\t\t\trequire XSLoader;\n\t\t\tXSLoader::load('Mousse', \$VERSION);\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Constructor::XS' }, ':meta');\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Destructor::XS' }, ':meta');\n\t\t\tMousse::Util->import({ into => 'Mousse::Meta::Method::Accessor::XS' }, ':meta');\n\t\t\treturn 1;\n\t\t"
Aresult
11
00
1103 local $^W = 0; # workaround 'redefine' warning to &install_subroutines
1104 require XSLoader;
1105 XSLoader::load('Mousse', $VERSION);
1106 Mousse::Util->import({ into => 'Mousse::Meta::Method::Constructor::XS' }, ':meta');
1107 Mousse::Util->import({ into => 'Mousse::Meta::Method::Destructor::XS' }, ':meta');
1108 Mousse::Util->import({ into => 'Mousse::Meta::Method::Accessor::XS' }, ':meta');
1109 return 1;
1110 } || 0;
1111 0 ▶ warn $@ if $@ && $ENV{MOUSE_XS};
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $ENV{"MOUSE_XS"} 0 0 0
Condition0%0/3
$@ and $ENV{"MOUSE_XS"}
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and $ENV{"MOUSE_XS"}
$@ $ENV{"MOUSE_XS"}
Truth table0%0/3
A and B
A= $@B= $ENV{"MOUSE_XS"}
ABresult
0X0
100
111
1112 }
1113  
1114 3 ▶ if(!$xs){
Branch: 1 branch
BranchTrueFalseTotal
unless ($xs) 3 0 3
1115 3 require 'Mousse/PurePerl.pm'; # we don't want to create its namespace
1116 }
1117  
1118 0 ▶ *MOUSE_XS = sub(){ $xs };
Subroutine __ANON__: not called CC=1 SCAR=1.2
1119  
1120 # definition of mro::get_linear_isa()
1121 0 my $get_linear_isa;
1122 0 ▶ if (eval { require mro }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require mro } }) { } 0 0 0
1123 0 $get_linear_isa = \&mro::get_linear_isa;
1124 }
1125 else {
1126 # this code is based on MRO::Compat::__get_linear_isa
1127 0 my $_get_linear_isa_dfs; # this recurses so it isn't pretty
1128 0 ▶ $_get_linear_isa_dfs = sub {
Subroutine __ANON__: not called CC=4 SCAR=25.3
1129 0 my($classname) = @_;
1130  
1131 0 my @lin = ($classname);
1132 0 my %stored;
1133  
1134 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1135 0 foreach my $parent (@{"$classname\::ISA"}) {
1136 0 foreach my $p(@{ $_get_linear_isa_dfs->($parent) }) {
1137 0 ▶ next if exists $stored{$p};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stored{$p} 0 0 0
1138 0 push(@lin, $p);
1139 0 $stored{$p} = 1;
1140 }
1141 }
1142 0 return \@lin;
1143 0 };
1144  
1145 {
1146 0 package # hide from PAUSE
1147 Class::C3;
1148 0 our %MRO; # avoid 'once' warnings
1149 }
1150  
1151 # MRO::Compat::__get_linear_isa has no prototype, so
1152 # we define a prototyped version for compatibility with core's
1153 # See also MRO::Compat::__get_linear_isa.
1154 0 ▶ $get_linear_isa = sub ($;$){
Subroutine __ANON__: not called CC=4 SCAR=30.0
1155 0 my($classname, $type) = @_;
1156  
1157 0 ▶ if(!defined $type){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 0 0
1158 0 ▶ $type = exists $Class::C3::MRO{$classname} ? 'c3' : 'dfs';
Branch: 1 branch
BranchTrueFalseTotal
exists $Class::C3::MRO{$classname} ? : 0 0 0
1159 }
1160 0 ▶ if($type eq 'c3'){
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'c3') { } 0 0 0
1161 0 require Class::C3;
1162 0 return [Class::C3::calculateMRO($classname)];
1163 }
1164 else{
1165 0 return $_get_linear_isa_dfs->($classname);
1166 }
1167 0 };
1168 }
1169  
1170 0 *get_linear_isa = $get_linear_isa;
1171 }
1172  
1173 use Carp ();
1174 use Scalar::Util ();
1175  
1176 # aliases as public APIs
1177 # it must be 'require', not 'use', because Mousse::Meta::Module depends on Mousse::Util
1178 require Mousse::Meta::Module; # for the entities of metaclass cache utilities
1179  
1180 # aliases
1181 {
1182 *class_of = \&Mousse::Meta::Module::_class_of;
1183 *get_metaclass_by_name = \&Mousse::Meta::Module::_get_metaclass_by_name;
1184 *get_all_metaclass_instances = \&Mousse::Meta::Module::_get_all_metaclass_instances;
1185 *get_all_metaclass_names = \&Mousse::Meta::Module::_get_all_metaclass_names;
1186  
1187 *Mousse::load_class = \&load_class;
1188 *Mousse::is_class_loaded = \&is_class_loaded;
1189  
1190 # is-a predicates
1191 #generate_isa_predicate_for('Mousse::Meta::TypeConstraint' => 'is_a_type_constraint');
1192 #generate_isa_predicate_for('Mousse::Meta::Class' => 'is_a_metaclass');
1193 #generate_isa_predicate_for('Mousse::Meta::Role' => 'is_a_metarole');
1194  
1195 # duck type predicates
1196 generate_can_predicate_for(['_compiled_type_constraint'] => 'is_a_type_constraint');
1197 generate_can_predicate_for(['create_anon_class'] => 'is_a_metaclass');
1198 generate_can_predicate_for(['create_anon_role'] => 'is_a_metarole');
1199 }
1200  
1201 our $in_global_destruction = 0;
1202 END{ $in_global_destruction = 1 }
1203  
1204 # Moose::Util compatible utilities
1205  
1206 sub find_meta{
1207 return class_of( $_[0] );
1208 }
1209  
1210 sub does_role{
1211 my ($class_or_obj, $role_name) = @_;
1212  
1213 my $meta = class_of($class_or_obj);
1214  
1215 (defined $role_name)
1216 || ($meta || 'Mousse::Meta::Class')->throw_error("You must supply a role name to does()");
1217  
1218 return defined($meta) && $meta->does_role($role_name);
1219 }
1220  
1221 # taken from Mousse::Util (0.90)
1222 {
1223 my %cache;
1224  
1225 sub resolve_metaclass_alias {
1226 my ( $type, $metaclass_name, %options ) = @_;
1227  
1228 my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' );
1229  
1230 return $cache{$cache_key}{$metaclass_name} ||= do{
1231  
1232 my $possible_full_name = join '::',
1233 'Mousse::Meta', $type, 'Custom', ($options{trait} ? 'Trait' : ()), $metaclass_name
1234 ;
1235  
1236 my $loaded_class = load_first_existing_class(
1237 $possible_full_name,
1238 $metaclass_name
1239 );
1240  
1241 $loaded_class->can('register_implementation')
1242 ? $loaded_class->register_implementation
1243 : $loaded_class;
1244 };
1245 }
1246 }
1247  
1248 # Utilities from Class::MOP
1249  
1250 sub get_code_info;
1251 sub get_code_package;
1252  
1253 sub is_valid_class_name;
1254 sub is_class_loaded;
1255  
1256 # taken from Class/MOP.pm
1257 sub load_first_existing_class {
1258 my @classes = @_
1259 or return;
1260  
1261 my %exceptions;
1262 for my $class (@classes) {
1263 my $e = _try_load_one_class($class);
1264  
1265 if ($e) {
1266 $exceptions{$class} = $e;
1267 }
1268 else {
1269 return $class;
1270 }
1271 }
1272  
1273 # not found
1274 Carp::confess join(
1275 "\n",
1276 map {
1277 sprintf( "Could not load class (%s) because : %s",
1278 $_, $exceptions{$_} )
1279 } @classes
1280 );
1281 }
1282  
1283 # taken from Class/MOP.pm
1284 sub _try_load_one_class {
1285 my $class = shift;
1286  
1287 unless ( is_valid_class_name($class) ) {
1288 my $display = defined($class) ? $class : 'undef';
1289 Carp::confess "Invalid class name ($display)";
1290 }
1291  
1292 return '' if is_class_loaded($class);
1293  
1294 $class =~ s{::}{/}g;
1295 $class .= '.pm';
1296  
1297 return do {
1298 local $@;
1299 eval { require $class };
1300 $@;
1301 };
1302 }
1303  
1304  
1305 sub load_class {
1306 my $class = shift;
1307 my $e = _try_load_one_class($class);
1308 Carp::confess "Could not load class ($class) because : $e" if $e;
1309  
1310 return $class;
1311 }
1312  
1313  
1314 sub apply_all_roles {
1315 my $consumer = Scalar::Util::blessed($_[0])
1316 ? $_[0] # instance
1317 : Mousse::Meta::Class->initialize($_[0]); # class or role name
1318  
1319 my @roles;
1320  
1321 # Basis of Data::OptList
1322 my $max = scalar(@_);
1323 for (my $i = 1; $i < $max ; $i++) {
1324 my $role = $_[$i];
1325 my $role_name;
1326 if(ref $role) {
1327 $role_name = $role->name;
1328 }
1329 else {
1330 $role_name = $role;
1331 load_class($role_name);
1332 $role = get_metaclass_by_name($role_name);
1333 }
1334  
1335 if ($i + 1 < $max && ref($_[$i + 1]) eq 'HASH') {
1336 push @roles, [ $role => $_[++$i] ];
1337 } else {
1338 push @roles, [ $role => undef ];
1339 }
1340 is_a_metarole($role)
1341 || $consumer->meta->throw_error("You can only consume roles, $role_name is not a Mousse role");
1342 }
1343  
1344 if ( scalar @roles == 1 ) {
1345 my ( $role, $params ) = @{ $roles[0] };
1346 $role->apply( $consumer, defined $params ? $params : () );
1347 }
1348 else {
1349 Mousse::Meta::Role->combine(@roles)->apply($consumer);
1350 }
1351 return;
1352 }
1353  
1354 # taken from Moose::Util 0.90
1355 sub english_list {
1356 return $_[0] if @_ == 1;
1357  
1358 my @items = sort @_;
1359  
1360 return "$items[0] and $items[1]" if @items == 2;
1361  
1362 my $tail = pop @items;
1363  
1364 return join q{, }, @items, "and $tail";
1365 }
1366  
1367 sub quoted_english_list {
1368 return english_list(map { qq{'$_'} } @_);
1369 }
1370  
1371 # common utilities
1372  
1373 sub not_supported{
1374 my($feature) = @_;
1375  
1376 $feature ||= ( caller(1) )[3] . '()'; # subroutine name
1377  
1378 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
1379 Carp::confess("Mousse does not currently support $feature");
1380 }
1381  
1382 # general meta() method
1383 sub meta :method{
1384 return Mousse::Meta::Class->initialize(ref($_[0]) || $_[0]);
1385 }
1386  
1387 # general throw_error() method
1388 # $o->throw_error($msg, depth => $leve, longmess => $croak_or_confess)
1389 sub throw_error :method {
1390 my($self, $message, %args) = @_;
1391  
1392 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
1393 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
1394  
1395 if(exists $args{longmess} && !$args{longmess}) {
1396 Carp::croak($message);
1397 }
1398 else{
1399 Carp::confess($message);
1400 }
1401 }
1402  
1403 # general dump() method
1404 sub dump :method {
1405 my($self, $maxdepth) = @_;
1406  
1407 require 'Data/Dumper.pm'; # we don't want to create its namespace
1408 my $dd = Data::Dumper->new([$self]);
1409 $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 3);
1410 $dd->Indent(1);
1411 $dd->Sortkeys(1);
1412 $dd->Quotekeys(0);
1413 return $dd->Dump();
1414 }
1415  
1416 # general does() method
1417 sub does :method {
1418 goto &does_role;
1419 }
1420  
1421 # Contents of Mouse::Meta::TypeConstraint
1422 package Mousse::Meta::TypeConstraint;
1423 use Mousse::Util qw(:meta); # enables strict and warnings
1424  
1425 sub new {
1426 my $class = shift;
1427 my %args = @_ == 1 ? %{$_[0]} : @_;
1428  
1429 $args{name} = '__ANON__' if !defined $args{name};
1430  
1431 my $type_parameter;
1432 if(defined $args{parent}) { # subtyping
1433 %args = (%{$args{parent}}, %args);
1434  
1435 # a child type must not inherit 'compiled_type_constraint'
1436 # and 'hand_optimized_type_constraint' from the parent
1437 delete $args{compiled_type_constraint}; # don't inherit it
1438 delete $args{hand_optimized_type_constraint}; # don't inherit it
1439  
1440 $type_parameter = $args{type_parameter};
1441 if(defined(my $parent_tp = $args{parent}{type_parameter})) {
1442 if($parent_tp != $type_parameter) {
1443 $type_parameter->is_a_type_of($parent_tp)
1444 or $class->throw_error(
1445 "$type_parameter is not a subtype of $parent_tp",
1446 );
1447 }
1448 else {
1449 $type_parameter = undef;
1450 }
1451 }
1452 }
1453  
1454 my $check;
1455  
1456 if($check = delete $args{optimized}) { # likely to be builtins
1457 $args{hand_optimized_type_constraint} = $check;
1458 $args{compiled_type_constraint} = $check;
1459 }
1460 elsif(defined $type_parameter) { # parameterizing
1461 my $generator = $args{constraint_generator}
1462 || $class->throw_error(
1463 "The $args{name} constraint cannot be used,"
1464 . " because $type_parameter doesn't subtype"
1465 . " from a parameterizable type");
1466  
1467 my $parameterized_check = $generator->($type_parameter);
1468 if(defined(my $my_check = $args{constraint})) {
1469 $check = sub {
1470 return $parameterized_check->($_) && $my_check->($_);
1471 };
1472 }
1473 else {
1474 $check = $parameterized_check;
1475 }
1476 $args{constraint} = $check;
1477 }
1478 else { # common cases
1479 $check = $args{constraint};
1480 }
1481  
1482 if(defined($check) && ref($check) ne 'CODE'){
1483 $class->throw_error(
1484 "Constraint for $args{name} is not a CODE reference");
1485 }
1486  
1487 my $self = bless \%args, $class;
1488 $self->compile_type_constraint()
1489 if !$args{hand_optimized_type_constraint};
1490  
1491 if($args{type_constraints}) { # union types
1492 foreach my $type(@{$self->{type_constraints}}){
1493 if($type->has_coercion){
1494 # set undef for has_coercion()
1495 $self->{_compiled_type_coercion} = undef;
1496 last;
1497 }
1498 }
1499 }
1500  
1501 return $self;
1502 }
1503  
1504 sub create_child_type {
1505 my $self = shift;
1506 return ref($self)->new(@_, parent => $self);
1507 }
1508  
1509 sub name;
1510 sub parent;
1511 sub message;
1512 sub has_coercion;
1513  
1514 sub check;
1515  
1516 sub type_parameter;
1517 sub __is_parameterized;
1518  
1519 sub _compiled_type_constraint;
1520 sub _compiled_type_coercion;
1521  
1522 sub compile_type_constraint;
1523  
1524  
1525 sub _add_type_coercions { # ($self, @pairs)
1526 my $self = shift;
1527  
1528 if(exists $self->{type_constraints}){ # union type
1529 $self->throw_error(
1530 "Cannot add additional type coercions to Union types '$self'");
1531 }
1532  
1533 my $coercion_map = ($self->{coercion_map} ||= []);
1534 my %has = map{ $_->[0]->name => undef } @{$coercion_map};
1535  
1536 for(my $i = 0; $i < @_; $i++){
1537 my $from = $_[ $i];
1538 my $action = $_[++$i];
1539  
1540 if(exists $has{$from}){
1541 $self->throw_error("A coercion action already exists for '$from'");
1542 }
1543  
1544 my $type = Mousse::Util::TypeConstraints::find_or_parse_type_constraint($from)
1545 or $self->throw_error(
1546 "Could not find the type constraint ($from) to coerce from");
1547  
1548 push @{$coercion_map}, [ $type => $action ];
1549 }
1550  
1551 $self->{_compiled_type_coercion} = undef;
1552 return;
1553 }
1554  
1555 sub _compiled_type_coercion {
1556 my($self) = @_;
1557  
1558 my $coercion = $self->{_compiled_type_coercion};
1559 return $coercion if defined $coercion;
1560  
1561 if(!$self->{type_constraints}) {
1562 my @coercions;
1563 foreach my $pair(@{$self->{coercion_map}}) {
1564 push @coercions,
1565 [ $pair->[0]->_compiled_type_constraint, $pair->[1] ];
1566 }
1567  
1568 $coercion = sub {
1569 my($thing) = @_;
1570 foreach my $pair (@coercions) {
1571 #my ($constraint, $converter) = @$pair;
1572 if ($pair->[0]->($thing)) {
1573 local $_ = $thing;
1574 return $pair->[1]->($thing);
1575 }
1576 }
1577 return $thing;
1578 };
1579 }
1580 else { # for union type
1581 my @coercions;
1582 foreach my $type(@{$self->{type_constraints}}){
1583 if($type->has_coercion){
1584 push @coercions, $type;
1585 }
1586 }
1587 if(@coercions){
1588 $coercion = sub {
1589 my($thing) = @_;
1590 foreach my $type(@coercions){
1591 my $value = $type->coerce($thing);
1592 return $value if $self->check($value);
1593 }
1594 return $thing;
1595 };
1596 }
1597 }
1598  
1599 return( $self->{_compiled_type_coercion} = $coercion );
1600 }
1601  
1602 sub coerce {
1603 my $self = shift;
1604 return $_[0] if $self->check(@_);
1605  
1606 my $coercion = $self->_compiled_type_coercion
1607 or $self->throw_error("Cannot coerce without a type coercion");
1608 return $coercion->(@_);
1609 }
1610  
1611 sub get_message {
1612 my ($self, $value) = @_;
1613 if ( my $msg = $self->message ) {
1614 local $_ = $value;
1615 return $msg->($value);
1616 }
1617 else {
1618 if(not defined $value) {
1619 $value = 'undef';
1620 }
1621 elsif( ref($value) && defined(&overload::StrVal) ) {
1622 $value = overload::StrVal($value);
1623 }
1624 return "Validation failed for '$self' with value $value";
1625 }
1626 }
1627  
1628 sub is_a_type_of {
1629 my($self, $other) = @_;
1630  
1631 # ->is_a_type_of('__ANON__') is always false
1632 return 0 if !ref($other) && $other eq '__ANON__';
1633  
1634 (my $other_name = $other) =~ s/\s+//g;
1635  
1636 return 1 if $self->name eq $other_name;
1637  
1638 if(exists $self->{type_constraints}){ # union
1639 foreach my $type(@{$self->{type_constraints}}) {
1640 return 1 if $type->name eq $other_name;
1641 }
1642 }
1643  
1644 for(my $p = $self->parent; defined $p; $p = $p->parent) {
1645 return 1 if $p->name eq $other_name;
1646 }
1647  
1648 return 0;
1649 }
1650  
1651 # See also Moose::Meta::TypeConstraint::Parameterizable
1652 sub parameterize {
1653 my($self, $param, $name) = @_;
1654  
1655 if(!ref $param){
1656 require Mousse::Util::TypeConstraints;
1657 $param = Mousse::Util::TypeConstraints::find_or_create_isa_type_constraint($param);
1658 }
1659  
1660 $name ||= sprintf '%s[%s]', $self->name, $param->name;
1661 return Mousse::Meta::TypeConstraint->new(
1662 name => $name,
1663 parent => $self,
1664 type_parameter => $param,
1665 );
1666 }
1667  
1668 sub assert_valid {
1669 my ($self, $value) = @_;
1670  
1671 if(!$self->check($value)){
1672 $self->throw_error($self->get_message($value));
1673 }
1674 return 1;
1675 }
1676  
1677 # overloading stuff
1678  
1679 sub _as_string { $_[0]->name } # overload ""
1680 sub _identity; # overload 0+
1681  
1682 sub _unite { # overload infix:<|>
1683 my($lhs, $rhs) = @_;
1684 require Mousse::Util::TypeConstraints;
1685 return Mousse::Util::TypeConstraints::_find_or_create_union_type(
1686 $lhs,
1687 Mousse::Util::TypeConstraints::find_or_create_isa_type_constraint($rhs),
1688 );
1689 }
1690  
1691 # Contents of Mouse::Util::TypeConstraints
1692 package Mousse::Util::TypeConstraints;
1693 use Mousse::Util; # enables strict and warnings
1694  
1695 use Mousse::Meta::TypeConstraint;
1696 use Mousse::Exporter;
1697  
1698 use Carp ();
1699 use Scalar::Util ();
1700  
1701 Mousse::Exporter->setup_import_methods(
1702 as_is => [qw(
1703 as where message optimize_as
1704 from via
1705  
1706 type subtype class_type role_type duck_type
1707 enum
1708 coerce
1709  
1710 find_type_constraint
1711 register_type_constraint
1712 )],
1713 );
1714  
1715 our @CARP_NOT = qw(Mousse::Meta::Attribute);
1716  
1717 my %TYPE;
1718  
1719 # The root type
1720 $TYPE{Any} = Mousse::Meta::TypeConstraint->new(
1721 name => 'Any',
1722 );
1723  
1724 my @builtins = (
1725 # $name => $parent, $code,
1726  
1727 # the base type
1728 Item => 'Any', undef,
1729  
1730 # the maybe[] type
1731 Maybe => 'Item', undef,
1732  
1733 # value types
1734 Undef => 'Item', \&Undef,
1735 Defined => 'Item', \&Defined,
1736 Bool => 'Item', \&Bool,
1737 Value => 'Defined', \&Value,
1738 Str => 'Value', \&Str,
1739 Num => 'Str', \&Num,
1740 Int => 'Num', \&Int,
1741  
1742 # ref types
1743 Ref => 'Defined', \&Ref,
1744 ScalarRef => 'Ref', \&ScalarRef,
1745 ArrayRef => 'Ref', \&ArrayRef,
1746 HashRef => 'Ref', \&HashRef,
1747 CodeRef => 'Ref', \&CodeRef,
1748 RegexpRef => 'Ref', \&RegexpRef,
1749 GlobRef => 'Ref', \&GlobRef,
1750  
1751 # object types
1752 FileHandle => 'GlobRef', \&FileHandle,
1753 Object => 'Ref', \&Object,
1754  
1755 # special string types
1756 ClassName => 'Str', \&ClassName,
1757 RoleName => 'ClassName', \&RoleName,
1758 );
1759  
1760 while (my ($name, $parent, $code) = splice @builtins, 0, 3) {
1761 $TYPE{$name} = Mousse::Meta::TypeConstraint->new(
1762 name => $name,
1763 parent => $TYPE{$parent},
1764 optimized => $code,
1765 );
1766 }
1767  
1768 # parametarizable types
1769 $TYPE{Maybe} {constraint_generator} = \&_parameterize_Maybe_for;
1770 $TYPE{ArrayRef}{constraint_generator} = \&_parameterize_ArrayRef_for;
1771 $TYPE{HashRef} {constraint_generator} = \&_parameterize_HashRef_for;
1772  
1773 # sugars
1774 sub as ($) { (as => $_[0]) } ## no critic
1775 sub where (&) { (where => $_[0]) } ## no critic
1776 sub message (&) { (message => $_[0]) } ## no critic
1777 sub optimize_as (&) { (optimize_as => $_[0]) } ## no critic
1778  
1779 sub from { @_ }
1780 sub via (&) { $_[0] } ## no critic
1781  
1782 # type utilities
1783  
1784 sub optimized_constraints { # DEPRECATED
1785 Carp::cluck('optimized_constraints() has been deprecated');
1786 return \%TYPE;
1787 }
1788  
1789 undef @builtins; # free the allocated memory
1790 @builtins = keys %TYPE; # reuse it
1791 sub list_all_builtin_type_constraints { @builtins }
1792 sub list_all_type_constraints { keys %TYPE }
1793  
1794 sub _define_type {
1795 my $is_subtype = shift;
1796 my $name;
1797 my %args;
1798  
1799 if(@_ == 1 && ref $_[0] ){ # @_ : { name => $name, where => ... }
1800 %args = %{$_[0]};
1801 }
1802 elsif(@_ == 2 && ref $_[1]) { # @_ : $name => { where => ... }
1803 $name = $_[0];
1804 %args = %{$_[1]};
1805 }
1806 elsif(@_ % 2) { # @_ : $name => ( where => ... )
1807 ($name, %args) = @_;
1808 }
1809 else{ # @_ : (name => $name, where => ...)
1810 %args = @_;
1811 }
1812  
1813 if(!defined $name){
1814 $name = $args{name};
1815 }
1816  
1817 $args{name} = $name;
1818  
1819 my $parent = delete $args{as};
1820 if($is_subtype && !$parent){
1821 $parent = delete $args{name};
1822 $name = undef;
1823 }
1824  
1825 if(defined $parent) {
1826 $args{parent} = find_or_create_isa_type_constraint($parent);
1827 }
1828  
1829 if(defined $name){
1830 # set 'package_defined_in' only if it is not a core package
1831 my $this = $args{package_defined_in};
1832 if(!$this){
1833 $this = caller(1);
1834 if($this !~ /\A Mousse \b/xms){
1835 $args{package_defined_in} = $this;
1836 }
1837 }
1838  
1839 if(defined $TYPE{$name}){
1840 my $that = $TYPE{$name}->{package_defined_in} || __PACKAGE__;
1841 if($this ne $that) {
1842 my $note = '';
1843 if($that eq __PACKAGE__) {
1844 $note = sprintf " ('%s' is %s type constraint)",
1845 $name,
1846 scalar(grep { $name eq $_ } list_all_builtin_type_constraints())
1847 ? 'a builtin'
1848 : 'an implicitly created';
1849 }
1850 Carp::croak("The type constraint '$name' has already been created in $that"
1851 . " and cannot be created again in $this" . $note);
1852 }
1853 }
1854 }
1855  
1856 $args{constraint} = delete $args{where} if exists $args{where};
1857 $args{optimized} = delete $args{optimized_as} if exists $args{optimized_as};
1858  
1859 my $constraint = Mousse::Meta::TypeConstraint->new(%args);
1860  
1861 if(defined $name){
1862 return $TYPE{$name} = $constraint;
1863 }
1864 else{
1865 return $constraint;
1866 }
1867 }
1868  
1869 sub type {
1870 return _define_type 0, @_;
1871 }
1872  
1873 sub subtype {
1874 return _define_type 1, @_;
1875 }
1876  
1877 sub coerce { # coerce $type, from $from, via { ... }, ...
1878 my $type_name = shift;
1879 my $type = find_type_constraint($type_name)
1880 or Carp::croak("Cannot find type '$type_name', perhaps you forgot to load it");
1881  
1882 $type->_add_type_coercions(@_);
1883 return;
1884 }
1885  
1886 sub class_type {
1887 my($name, $options) = @_;
1888 my $class = $options->{class} || $name;
1889  
1890 # ClassType
1891 return subtype $name => (
1892 as => 'Object',
1893 optimized_as => Mousse::Util::generate_isa_predicate_for($class),
1894 class => $class,
1895 );
1896 }
1897  
1898 sub role_type {
1899 my($name, $options) = @_;
1900 my $role = $options->{role} || $name;
1901  
1902 # RoleType
1903 return subtype $name => (
1904 as => 'Object',
1905 optimized_as => sub {
1906 return Scalar::Util::blessed($_[0])
1907 && Mousse::Util::does_role($_[0], $role);
1908 },
1909 role => $role,
1910 );
1911 }
1912  
1913 sub duck_type {
1914 my($name, @methods);
1915  
1916 if(ref($_[0]) ne 'ARRAY'){
1917 $name = shift;
1918 }
1919  
1920 @methods = (@_ == 1 && ref($_[0]) eq 'ARRAY') ? @{$_[0]} : @_;
1921  
1922 # DuckType
1923 return _define_type 1, $name => (
1924 as => 'Object',
1925 optimized_as => Mousse::Util::generate_can_predicate_for(\@methods),
1926 message => sub {
1927 my($object) = @_;
1928 my @missing = grep { !$object->can($_) } @methods;
1929 return ref($object)
1930 . ' is missing methods '
1931 . Mousse::Util::quoted_english_list(@missing);
1932 },
1933 methods => \@methods,
1934 );
1935 }
1936  
1937 sub enum {
1938 my($name, %valid);
1939  
1940 if(!(@_ == 1 && ref($_[0]) eq 'ARRAY')){
1941 $name = shift;
1942 }
1943  
1944 %valid = map{ $_ => undef }
1945 (@_ == 1 && ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_);
1946  
1947 # EnumType
1948 return _define_type 1, $name => (
1949 as => 'Str',
1950 optimized_as => sub{
1951 return defined($_[0]) && !ref($_[0]) && exists $valid{$_[0]};
1952 },
1953 );
1954 }
1955  
1956 sub _find_or_create_regular_type{
1957 my($spec, $create) = @_;
1958  
1959 return $TYPE{$spec} if exists $TYPE{$spec};
1960  
1961 my $meta = Mousse::Util::get_metaclass_by_name($spec);
1962  
1963 if(!defined $meta){
1964 return $create ? class_type($spec) : undef;
1965 }
1966  
1967 if(Mousse::Util::is_a_metarole($meta)){
1968 return role_type($spec);
1969 }
1970 else{
1971 return class_type($spec);
1972 }
1973 }
1974  
1975 sub _find_or_create_parameterized_type{
1976 my($base, $param) = @_;
1977  
1978 my $name = sprintf '%s[%s]', $base->name, $param->name;
1979  
1980 $TYPE{$name} ||= $base->parameterize($param, $name);
1981 }
1982  
1983 sub _find_or_create_union_type{
1984 return if grep{ not defined } @_; # all things must be defined
1985 my @types = sort
1986 map{ $_->{type_constraints} ? @{$_->{type_constraints}} : $_ } @_;
1987  
1988 my $name = join '|', @types;
1989  
1990 # UnionType
1991 $TYPE{$name} ||= Mousse::Meta::TypeConstraint->new(
1992 name => $name,
1993 type_constraints => \@types,
1994 );
1995 }
1996  
1997 # The type parser
1998  
1999 # param : '[' type ']' | NOTHING
2000 sub _parse_param {
2001 my($c) = @_;
2002  
2003 if($c->{spec} =~ s/^\[//){
2004 my $type = _parse_type($c, 1);
2005  
2006 if($c->{spec} =~ s/^\]//){
2007 return $type;
2008 }
2009 Carp::croak("Syntax error in type: missing right square bracket in '$c->{orig}'");
2010 }
2011  
2012 return undef;
2013 }
2014  
2015 # name : [\w.:]+
2016 sub _parse_name {
2017 my($c, $create) = @_;
2018  
2019 if($c->{spec} =~ s/\A ([\w.:]+) //xms){
2020 return _find_or_create_regular_type($1, $create);
2021 }
2022 Carp::croak("Syntax error in type: expect type name near '$c->{spec}' in '$c->{orig}'");
2023 }
2024  
2025 # single_type : name param
2026 sub _parse_single_type {
2027 my($c, $create) = @_;
2028  
2029 my $type = _parse_name($c, $create);
2030 my $param = _parse_param($c);
2031  
2032 if(defined $type){
2033 if(defined $param){
2034 return _find_or_create_parameterized_type($type, $param);
2035 }
2036 else {
2037 return $type;
2038 }
2039 }
2040 elsif(defined $param){
2041 Carp::croak("Undefined type with parameter [$param] in '$c->{orig}'");
2042 }
2043 else{
2044 return undef;
2045 }
2046 }
2047  
2048 # type : single_type ('|' single_type)*
2049 sub _parse_type {
2050 my($c, $create) = @_;
2051  
2052 my $type = _parse_single_type($c, $create);
2053 if($c->{spec}){ # can be an union type
2054 my @types;
2055 while($c->{spec} =~ s/^\|//){
2056 push @types, _parse_single_type($c, $create);
2057 }
2058 if(@types){
2059 return _find_or_create_union_type($type, @types);
2060 }
2061 }
2062 return $type;
2063 }
2064  
2065  
2066 sub find_type_constraint {
2067 my($spec) = @_;
2068 return $spec if Mousse::Util::is_a_type_constraint($spec) or not defined $spec;
2069  
2070 $spec =~ s/\s+//g;
2071 return $TYPE{$spec};
2072 }
2073  
2074 sub register_type_constraint {
2075 my($constraint) = @_;
2076 Carp::croak("No type supplied / type is not a valid type constraint")
2077 unless Mousse::Util::is_a_type_constraint($constraint);
2078 return $TYPE{$constraint->name} = $constraint;
2079 }
2080  
2081 sub find_or_parse_type_constraint {
2082 my($spec) = @_;
2083 return $spec if Mousse::Util::is_a_type_constraint($spec) or not defined $spec;
2084  
2085 $spec =~ tr/ \t\r\n//d;
2086  
2087 my $tc = $TYPE{$spec};
2088 if(defined $tc) {
2089 return $tc;
2090 }
2091  
2092 my %context = (
2093 spec => $spec,
2094 orig => $spec,
2095 );
2096 $tc = _parse_type(\%context);
2097  
2098 if($context{spec}){
2099 Carp::croak("Syntax error: extra elements '$context{spec}' in '$context{orig}'");
2100 }
2101  
2102 return $TYPE{$spec} = $tc;
2103 }
2104  
2105 sub find_or_create_does_type_constraint{
2106 # XXX: Moose does not register a new role_type, but Mousse does.
2107 my $tc = find_or_parse_type_constraint(@_);
2108 return defined($tc) ? $tc : role_type(@_);
2109 }
2110  
2111 sub find_or_create_isa_type_constraint {
2112 # XXX: Moose does not register a new class_type, but Mousse does.
2113 my $tc = find_or_parse_type_constraint(@_);
2114 return defined($tc) ? $tc : class_type(@_);
2115 }
2116  
2117 # Contents of Mouse
2118 package Mousse::TOP;
2119 use 5.006_002;
2120  
2121 use Mousse::Exporter; # enables strict and warnings
2122  
2123 our $VERSION = '0.93';
2124  
2125 use Carp ();
2126 use Scalar::Util ();
2127  
2128 use Mousse::Util ();
2129  
2130 use Mousse::Meta::Module;
2131 use Mousse::Meta::Class;
2132 use Mousse::Meta::Role;
2133 use Mousse::Meta::Attribute;
2134 use Mousse::Object;
2135 use Mousse::Util::TypeConstraints ();
2136  
2137 Mousse::Exporter->setup_import_methods(
2138 as_is => [qw(
2139 extends with
2140 has
2141 before after around
2142 override super
2143 augment inner
2144 ),
2145 \&Scalar::Util::blessed,
2146 \&Carp::confess,
2147 ],
2148 );
2149  
2150  
2151 sub extends {
2152 Mousse::Meta::Class->initialize(scalar caller)->superclasses(@_);
2153 return;
2154 }
2155  
2156 sub with {
2157 Mousse::Util::apply_all_roles(scalar(caller), @_);
2158 return;
2159 }
2160  
2161 sub has {
2162 my $meta = Mousse::Meta::Class->initialize(scalar caller);
2163 my $name = shift;
2164  
2165 $meta->throw_error(q{Usage: has 'name' => ( key => value, ... )})
2166 if @_ % 2; # odd number of arguments
2167  
2168 for my $n(ref($name) ? @{$name} : $name){
2169 $meta->add_attribute($n => @_);
2170 }
2171 return;
2172 }
2173  
2174 sub before {
2175 my $meta = Mousse::Meta::Class->initialize(scalar caller);
2176 my $code = pop;
2177 for my $name($meta->_collect_methods(@_)) {
2178 $meta->add_before_method_modifier($name => $code);
2179 }
2180 return;
2181 }
2182  
2183 sub after {
2184 my $meta = Mousse::Meta::Class->initialize(scalar caller);
2185 my $code = pop;
2186 for my $name($meta->_collect_methods(@_)) {
2187 $meta->add_after_method_modifier($name => $code);
2188 }
2189 return;
2190 }
2191  
2192 sub around {
2193 my $meta = Mousse::Meta::Class->initialize(scalar caller);
2194 my $code = pop;
2195 for my $name($meta->_collect_methods(@_)) {
2196 $meta->add_around_method_modifier($name => $code);
2197 }
2198 return;
2199 }
2200  
2201 our $SUPER_PACKAGE;
2202 our $SUPER_BODY;
2203 our @SUPER_ARGS;
2204  
2205 sub super {
2206 # This check avoids a recursion loop - see
2207 # t/100_bugs/020_super_recursion.t
2208 return if defined $SUPER_PACKAGE && $SUPER_PACKAGE ne caller();
2209 return if !defined $SUPER_BODY;
2210 $SUPER_BODY->(@SUPER_ARGS);
2211 }
2212  
2213 sub override {
2214 # my($name, $method) = @_;
2215 Mousse::Meta::Class->initialize(scalar caller)->add_override_method_modifier(@_);
2216 }
2217  
2218 our %INNER_BODY;
2219 our %INNER_ARGS;
2220  
2221 sub inner {
2222 my $pkg = caller();
2223 if ( my $body = $INNER_BODY{$pkg} ) {
2224 my $args = $INNER_ARGS{$pkg};
2225 local $INNER_ARGS{$pkg};
2226 local $INNER_BODY{$pkg};
2227 return $body->(@{$args});
2228 }
2229 else {
2230 return;
2231 }
2232 }
2233  
2234 sub augment {
2235 #my($name, $method) = @_;
2236 Mousse::Meta::Class->initialize(scalar caller)->add_augment_method_modifier(@_);
2237 return;
2238 }
2239  
2240 sub init_meta {
2241 shift;
2242 my %args = @_;
2243  
2244 my $class = $args{for_class}
2245 or confess("Cannot call init_meta without specifying a for_class");
2246  
2247 my $base_class = $args{base_class} || 'Mousse::Object';
2248 my $metaclass = $args{metaclass} || 'Mousse::Meta::Class';
2249  
2250 my $meta = $metaclass->initialize($class);
2251  
2252 $meta->add_method(meta => sub{
2253 return $metaclass->initialize(ref($_[0]) || $_[0]);
2254 });
2255  
2256 $meta->superclasses($base_class)
2257 unless $meta->superclasses;
2258  
2259 # make a class type for each Mousse class
2260 Mousse::Util::TypeConstraints::class_type($class)
2261 unless Mousse::Util::TypeConstraints::find_type_constraint($class);
2262  
2263 return $meta;
2264 }
2265  
2266 # Contents of Mouse::Meta::Attribute
2267 package Mousse::Meta::Attribute;
2268 use Mousse::Util qw(:meta); # enables strict and warnings
2269  
2270 use Carp ();
2271  
2272 use Mousse::Meta::TypeConstraint;
2273  
2274 my %valid_options = map { $_ => undef } (
2275 'accessor',
2276 'auto_deref',
2277 'builder',
2278 'clearer',
2279 'coerce',
2280 'default',
2281 'documentation',
2282 'does',
2283 'handles',
2284 'init_arg',
2285 'insertion_order',
2286 'is',
2287 'isa',
2288 'lazy',
2289 'lazy_build',
2290 'name',
2291 'predicate',
2292 'reader',
2293 'required',
2294 'traits',
2295 'trigger',
2296 'type_constraint',
2297 'weak_ref',
2298 'writer',
2299  
2300 # internally used
2301 'associated_class',
2302 'associated_methods',
2303 '__METACLASS__',
2304  
2305 # Moose defines, but Mousse doesn't
2306 #'definition_context',
2307 #'initializer',
2308  
2309 # special case for AttributeHelpers
2310 'provides',
2311 'curries',
2312 );
2313  
2314 our @CARP_NOT = qw(Mousse::Meta::Class);
2315  
2316 sub new {
2317 my $class = shift;
2318 my $name = shift;
2319  
2320 my $args = $class->Mousse::Object::BUILDARGS(@_);
2321  
2322 $class->_process_options($name, $args);
2323  
2324 $args->{name} = $name;
2325  
2326 # check options
2327 # (1) known by core
2328 my @bad = grep{ !exists $valid_options{$_} } keys %{$args};
2329  
2330 # (2) known by subclasses
2331 if(@bad && $class ne __PACKAGE__){
2332 my %valid_attrs = (
2333 map { $_ => undef }
2334 grep { defined }
2335 map { $_->init_arg() }
2336 $class->meta->get_all_attributes()
2337 );
2338 @bad = grep{ !exists $valid_attrs{$_} } @bad;
2339 }
2340  
2341 # (3) bad options found
2342 if(@bad){
2343 Carp::carp(
2344 "Found unknown argument(s) passed to '$name' attribute constructor in '$class': "
2345 . Mousse::Util::english_list(@bad));
2346 }
2347  
2348 my $self = bless $args, $class;
2349 if($class ne __PACKAGE__){
2350 $class->meta->_initialize_object($self, $args);
2351 }
2352 return $self;
2353 }
2354  
2355 sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor }
2356 sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
2357  
2358 sub get_read_method { $_[0]->reader || $_[0]->accessor }
2359 sub get_write_method { $_[0]->writer || $_[0]->accessor }
2360  
2361 sub get_read_method_ref{
2362 my($self) = @_;
2363 return $self->{_mouse_cache_read_method_ref}
2364 ||= $self->_get_accessor_method_ref('get_read_method', '_generate_reader');
2365 }
2366  
2367 sub get_write_method_ref{
2368 my($self) = @_;
2369 return $self->{_mouse_cache_write_method_ref}
2370 ||= $self->_get_accessor_method_ref('get_write_method', '_generate_writer');
2371 }
2372  
2373 sub interpolate_class{
2374 my($class, $args) = @_;
2375  
2376 if(my $metaclass = delete $args->{metaclass}){
2377 $class = Mousse::Util::resolve_metaclass_alias( Attribute => $metaclass );
2378 }
2379  
2380 my @traits;
2381 if(my $traits_ref = delete $args->{traits}){
2382  
2383 for (my $i = 0; $i < @{$traits_ref}; $i++) {
2384 my $trait = Mousse::Util::resolve_metaclass_alias(Attribute => $traits_ref->[$i], trait => 1);
2385  
2386 next if $class->does($trait);
2387  
2388 push @traits, $trait;
2389  
2390 # are there options?
2391 push @traits, $traits_ref->[++$i]
2392 if ref($traits_ref->[$i+1]);
2393 }
2394  
2395 if (@traits) {
2396 $class = Mousse::Meta::Class->create_anon_class(
2397 superclasses => [ $class ],
2398 roles => \@traits,
2399 cache => 1,
2400 )->name;
2401 }
2402 }
2403  
2404 return( $class, @traits );
2405 }
2406  
2407 sub verify_against_type_constraint {
2408 my ($self, $value) = @_;
2409  
2410 my $type_constraint = $self->{type_constraint};
2411 return 1 if !$type_constraint;
2412 return 1 if $type_constraint->check($value);
2413  
2414 $self->_throw_type_constraint_error($value, $type_constraint);
2415 }
2416  
2417 sub _throw_type_constraint_error {
2418 my($self, $value, $type) = @_;
2419  
2420 $self->throw_error(
2421 sprintf q{Attribute (%s) does not pass the type constraint because: %s},
2422 $self->name,
2423 $type->get_message($value),
2424 );
2425 }
2426  
2427 sub illegal_options_for_inheritance {
2428 return qw(reader writer accessor clearer predicate);
2429 }
2430  
2431 sub clone_and_inherit_options{
2432 my $self = shift;
2433 my $args = $self->Mousse::Object::BUILDARGS(@_);
2434  
2435 foreach my $illegal($self->illegal_options_for_inheritance) {
2436 if(exists $args->{$illegal} and exists $self->{$illegal}) {
2437 $self->throw_error("Illegal inherited option: $illegal");
2438 }
2439 }
2440  
2441 foreach my $name(keys %{$self}){
2442 if(!exists $args->{$name}){
2443 $args->{$name} = $self->{$name}; # inherit from self
2444 }
2445 }
2446  
2447 my($attribute_class, @traits) = ref($self)->interpolate_class($args);
2448 $args->{traits} = \@traits if @traits;
2449  
2450 # remove temporary caches
2451 foreach my $attr(keys %{$args}){
2452 if($attr =~ /\A _mouse_cache_/xms){
2453 delete $args->{$attr};
2454 }
2455 }
2456  
2457 # remove default if lazy_build => 1
2458 if($args->{lazy_build}) {
2459 delete $args->{default};
2460 }
2461  
2462 return $attribute_class->new($self->name, $args);
2463 }
2464  
2465  
2466 sub _get_accessor_method_ref {
2467 my($self, $type, $generator) = @_;
2468  
2469 my $metaclass = $self->associated_class
2470 || $self->throw_error('No asocciated class for ' . $self->name);
2471  
2472 my $accessor = $self->$type();
2473 if($accessor){
2474 return $metaclass->get_method_body($accessor);
2475 }
2476 else{
2477 return $self->accessor_metaclass->$generator($self, $metaclass);
2478 }
2479 }
2480  
2481 sub set_value {
2482 my($self, $object, $value) = @_;
2483 return $self->get_write_method_ref()->($object, $value);
2484 }
2485  
2486 sub get_value {
2487 my($self, $object) = @_;
2488 return $self->get_read_method_ref()->($object);
2489 }
2490  
2491 sub has_value {
2492 my($self, $object) = @_;
2493 my $accessor_ref = $self->{_mouse_cache_predicate_ref}
2494 ||= $self->_get_accessor_method_ref('predicate', '_generate_predicate');
2495  
2496 return $accessor_ref->($object);
2497 }
2498  
2499 sub clear_value {
2500 my($self, $object) = @_;
2501 my $accessor_ref = $self->{_mouse_cache_crealer_ref}
2502 ||= $self->_get_accessor_method_ref('clearer', '_generate_clearer');
2503  
2504 return $accessor_ref->($object);
2505 }
2506  
2507 sub associate_method{
2508 #my($attribute, $method_name) = @_;
2509 my($attribute) = @_;
2510 $attribute->{associated_methods}++;
2511 return;
2512 }
2513  
2514 sub install_accessors{
2515 my($attribute) = @_;
2516  
2517 my $metaclass = $attribute->associated_class;
2518 my $accessor_class = $attribute->accessor_metaclass;
2519  
2520 foreach my $type(qw(accessor reader writer predicate clearer)){
2521 if(exists $attribute->{$type}){
2522 my $generator = '_generate_' . $type;
2523 my $code = $accessor_class->$generator($attribute, $metaclass);
2524 my $name = $attribute->{$type};
2525 # TODO: do something for compatibility
2526 # if( $metaclass->name->can($name) ) {
2527 # my $t = $metaclass->has_method($name) ? 'method' : 'function';
2528 # Carp::cluck("You are overwriting a locally defined $t"
2529 # . " ($name) with an accessor");
2530 # }
2531 $metaclass->add_method($name => $code);
2532 $attribute->associate_method($name);
2533 }
2534 }
2535  
2536 # install delegation
2537 if(exists $attribute->{handles}){
2538 my %handles = $attribute->_canonicalize_handles();
2539 while(my($handle, $method_to_call) = each %handles){
2540 next if Mousse::Object->can($handle);
2541  
2542 if($metaclass->has_method($handle)) {
2543 $attribute->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation");
2544 }
2545  
2546 $metaclass->add_method($handle =>
2547 $attribute->_make_delegation_method(
2548 $handle, $method_to_call));
2549  
2550 $attribute->associate_method($handle);
2551 }
2552 }
2553  
2554 return;
2555 }
2556  
2557 sub delegation_metaclass() { ## no critic
2558 'Mousse::Meta::Method::Delegation'
2559 }
2560  
2561 sub _canonicalize_handles {
2562 my($self) = @_;
2563 my $handles = $self->{handles};
2564  
2565 my $handle_type = ref $handles;
2566 if ($handle_type eq 'HASH') {
2567 return %$handles;
2568 }
2569 elsif ($handle_type eq 'ARRAY') {
2570 return map { $_ => $_ } @$handles;
2571 }
2572 elsif ($handle_type eq 'Regexp') {
2573 my $meta = $self->_find_delegate_metaclass();
2574 return map { $_ => $_ }
2575 grep { /$handles/ }
2576 Mousse::Util::is_a_metarole($meta)
2577 ? $meta->get_method_list
2578 : $meta->get_all_method_names;
2579 }
2580 elsif ($handle_type eq 'CODE') {
2581 return $handles->( $self, $self->_find_delegate_metaclass() );
2582 }
2583 else {
2584 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
2585 }
2586 }
2587  
2588 sub _find_delegate_metaclass {
2589 my($self) = @_;
2590 my $meta;
2591 if($self->{isa}) {
2592 $meta = Mousse::Meta::Class->initialize("$self->{isa}");
2593 }
2594 elsif($self->{does}) {
2595 $meta = Mousse::Util::get_metaclass_by_name("$self->{does}");
2596 }
2597 defined($meta) or $self->throw_error(
2598 "Cannot find delegate metaclass for attribute " . $self->name);
2599 return $meta;
2600 }
2601  
2602  
2603 sub _make_delegation_method {
2604 my($self, $handle, $method_to_call) = @_;
2605 return Mousse::Util::load_class($self->delegation_metaclass)
2606 ->_generate_delegation($self, $handle, $method_to_call);
2607 }
2608  
2609 # Contents of Mouse::Meta::Class
2610 package Mousse::Meta::Class;
2611 use Mousse::Util qw/:meta/; # enables strict and warnings
2612 no warnings 'once';
2613  
2614 use Scalar::Util ();
2615  
2616 use Mousse::Meta::Module;
2617 our @ISA = qw(Mousse::Meta::Module);
2618  
2619 our @CARP_NOT = qw(Mousse); # trust Mousse
2620  
2621 sub attribute_metaclass;
2622 sub method_metaclass;
2623  
2624 sub constructor_class;
2625 sub destructor_class;
2626  
2627  
2628 sub _construct_meta {
2629 my($class, %args) = @_;
2630  
2631 $args{attributes} = {};
2632 $args{methods} = {};
2633 $args{roles} = [];
2634  
2635 $args{superclasses} = do {
2636 no strict 'refs';
2637 \@{ $args{package} . '::ISA' };
2638 };
2639  
2640 my $self = bless \%args, ref($class) || $class;
2641 if(ref($self) ne __PACKAGE__){
2642 $self->meta->_initialize_object($self, \%args);
2643 }
2644 return $self;
2645 }
2646  
2647 sub create_anon_class{
2648 my $self = shift;
2649 return $self->create(undef, @_);
2650 }
2651  
2652 sub is_anon_class;
2653  
2654 sub roles;
2655  
2656 sub calculate_all_roles {
2657 my $self = shift;
2658 my %seen;
2659 return grep { !$seen{ $_->name }++ }
2660 map { $_->calculate_all_roles } @{ $self->roles };
2661 }
2662  
2663 sub superclasses {
2664 my $self = shift;
2665  
2666 if (@_) {
2667 foreach my $super(@_){
2668 Mousse::Util::load_class($super);
2669 my $meta = Mousse::Util::get_metaclass_by_name($super);
2670 next if $self->verify_superclass($super, $meta);
2671 $self->_reconcile_with_superclass_meta($meta);
2672 }
2673 return @{ $self->{superclasses} } = @_;
2674 }
2675  
2676 return @{ $self->{superclasses} };
2677 }
2678  
2679 sub verify_superclass {
2680 my($self, $super, $super_meta) = @_;
2681  
2682 if(defined $super_meta) {
2683 if(Mousse::Util::is_a_metarole($super_meta)){
2684 $self->throw_error("You cannot inherit from a Mousse Role ($super)");
2685 }
2686 }
2687 else {
2688 # The metaclass of $super is not initialized.
2689 # i.e. it might be Mousse::Object, a mixin package (e.g. Exporter),
2690 # or a foreign class including Moose classes.
2691 # See also Mousse::Foreign::Meta::Role::Class.
2692 my $mm = $super->can('meta');
2693 if(!($mm && $mm == \&Mousse::Util::meta)) {
2694 if($super->can('new') or $super->can('DESTROY')) {
2695 $self->inherit_from_foreign_class($super);
2696 }
2697 }
2698 return 1; # always ok
2699 }
2700  
2701 return $self->isa(ref $super_meta); # checks metaclass compatibility
2702 }
2703  
2704 sub inherit_from_foreign_class {
2705 my($class, $super) = @_;
2706 if($ENV{PERL_MOUSE_STRICT}) {
2707 Carp::carp("You inherit from non-Mousse class ($super),"
2708 . " but it is unlikely to work correctly."
2709 . " Please consider using MousseX::Foreign");
2710 }
2711 return;
2712 }
2713  
2714 my @MetaClassTypes = (
2715 'attribute', # Mousse::Meta::Attribute
2716 'method', # Mousse::Meta::Method
2717 'constructor', # Mousse::Meta::Method::Constructor
2718 'destructor', # Mousse::Meta::Method::Destructor
2719 );
2720  
2721 sub _reconcile_with_superclass_meta {
2722 my($self, $other) = @_;
2723  
2724 # find incompatible traits
2725 my %metaroles;
2726 foreach my $metaclass_type(@MetaClassTypes){
2727 my $accessor = $self->can($metaclass_type . '_metaclass')
2728 || $self->can($metaclass_type . '_class');
2729  
2730 my $other_c = $other->$accessor();
2731 my $self_c = $self->$accessor();
2732  
2733 if(!$self_c->isa($other_c)){
2734 $metaroles{$metaclass_type}
2735 = [ $self_c->meta->_collect_roles($other_c->meta) ];
2736 }
2737 }
2738  
2739 $metaroles{class} = [$self->meta->_collect_roles($other->meta)];
2740  
2741 #use Data::Dumper; print Data::Dumper->new([\%metaroles], ['*metaroles'])->Indent(1)->Dump;
2742  
2743 require Mousse::Util::MetaRole;
2744 $_[0] = Mousse::Util::MetaRole::apply_metaroles(
2745 for => $self,
2746 class_metaroles => \%metaroles,
2747 );
2748 return;
2749 }
2750  
2751 sub _collect_roles {
2752 my ($self, $other) = @_;
2753  
2754 # find common ancestor
2755 my @self_lin_isa = $self->linearized_isa;
2756 my @other_lin_isa = $other->linearized_isa;
2757  
2758 my(@self_anon_supers, @other_anon_supers);
2759 push @self_anon_supers, shift @self_lin_isa while $self_lin_isa[0]->meta->is_anon_class;
2760 push @other_anon_supers, shift @other_lin_isa while $other_lin_isa[0]->meta->is_anon_class;
2761  
2762 my $common_ancestor = $self_lin_isa[0] eq $other_lin_isa[0] && $self_lin_isa[0];
2763  
2764 if(!$common_ancestor){
2765 $self->throw_error(sprintf '%s cannot have %s as a super class because of their metaclass incompatibility',
2766 $self->name, $other->name);
2767 }
2768  
2769 my %seen;
2770 return sort grep { !$seen{$_}++ } ## no critic
2771 (map{ $_->name } map{ $_->meta->calculate_all_roles } @self_anon_supers),
2772 (map{ $_->name } map{ $_->meta->calculate_all_roles } @other_anon_supers),
2773 ;
2774 }
2775  
2776  
2777 sub find_method_by_name {
2778 my($self, $method_name) = @_;
2779 defined($method_name)
2780 or $self->throw_error('You must define a method name to find');
2781  
2782 foreach my $class( $self->linearized_isa ){
2783 my $method = $self->initialize($class)->get_method($method_name);
2784 return $method if defined $method;
2785 }
2786 return undef;
2787 }
2788  
2789 sub get_all_methods {
2790 my($self) = @_;
2791 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
2792 }
2793  
2794 sub get_all_method_names {
2795 my $self = shift;
2796 my %uniq;
2797 return grep { $uniq{$_}++ == 0 }
2798 map { Mousse::Meta::Class->initialize($_)->get_method_list() }
2799 $self->linearized_isa;
2800 }
2801  
2802 sub find_attribute_by_name {
2803 my($self, $name) = @_;
2804 defined($name)
2805 or $self->throw_error('You must define an attribute name to find');
2806 foreach my $attr($self->get_all_attributes) {
2807 return $attr if $attr->name eq $name;
2808 }
2809 return undef;
2810 }
2811  
2812 sub add_attribute {
2813 my $self = shift;
2814  
2815 my($attr, $name);
2816  
2817 if(Scalar::Util::blessed($_[0])){
2818 $attr = $_[0];
2819  
2820 $attr->isa('Mousse::Meta::Attribute')
2821 || $self->throw_error("Your attribute must be an instance of Mousse::Meta::Attribute (or a subclass)");
2822  
2823 $name = $attr->name;
2824 }
2825 else{
2826 # _process_attribute
2827 $name = shift;
2828  
2829 my %args = (@_ == 1) ? %{$_[0]} : @_;
2830  
2831 defined($name)
2832 or $self->throw_error('You must provide a name for the attribute');
2833  
2834 if ($name =~ s/^\+//) { # inherited attributes
2835 my $inherited_attr = $self->find_attribute_by_name($name)
2836 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
2837  
2838 $attr = $inherited_attr->clone_and_inherit_options(%args);
2839 }
2840 else{
2841 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
2842 $args{traits} = \@traits if @traits;
2843  
2844 $attr = $attribute_class->new($name, %args);
2845 }
2846 }
2847  
2848 Scalar::Util::weaken( $attr->{associated_class} = $self );
2849  
2850 # install accessors first
2851 $attr->install_accessors();
2852  
2853 # then register the attribute to the metaclass
2854 $attr->{insertion_order} = keys %{ $self->{attributes} };
2855 $self->{attributes}{$name} = $attr;
2856 $self->_invalidate_metaclass_cache();
2857  
2858 if(!$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
2859 Carp::carp(qq{Attribute ($name) of class }.$self->name
2860 .qq{ has no associated methods (did you mean to provide an "is" argument?)});
2861 }
2862 return $attr;
2863 }
2864  
2865 sub _calculate_all_attributes {
2866 my($self) = @_;
2867 my %seen;
2868 my @all_attrs;
2869 foreach my $class($self->linearized_isa) {
2870 my $meta = Mousse::Util::get_metaclass_by_name($class) or next;
2871 my @attrs = grep { !$seen{$_->name}++ } values %{$meta->{attributes}};
2872 @attrs = sort {
2873 $b->{insertion_order} <=> $a->{insertion_order}
2874 } @attrs;
2875 push @all_attrs, @attrs;
2876 }
2877 return [reverse @all_attrs];
2878 }
2879  
2880 sub linearized_isa;
2881  
2882 sub new_object;
2883 sub clone_object;
2884  
2885 sub immutable_options {
2886 my ( $self, @args ) = @_;
2887  
2888 return (
2889 inline_constructor => 1,
2890 inline_destructor => 1,
2891 constructor_name => 'new',
2892 @args,
2893 );
2894 }
2895  
2896 sub make_immutable {
2897 my $self = shift;
2898 my %args = $self->immutable_options(@_);
2899  
2900 $self->{is_immutable}++;
2901  
2902 if ($args{inline_constructor}) {
2903 $self->add_method($args{constructor_name} =>
2904 Mousse::Util::load_class($self->constructor_class)
2905 ->_generate_constructor($self, \%args));
2906 }
2907  
2908 if ($args{inline_destructor}) {
2909 $self->add_method(DESTROY =>
2910 Mousse::Util::load_class($self->destructor_class)
2911 ->_generate_destructor($self, \%args));
2912 }
2913  
2914 # Moose's make_immutable returns true allowing calling code to skip
2915 # setting an explicit true value at the end of a source file.
2916 return 1;
2917 }
2918  
2919 sub make_mutable {
2920 my($self) = @_;
2921 $self->{is_immutable} = 0;
2922 return;
2923 }
2924  
2925 sub is_immutable;
2926 sub is_mutable { !$_[0]->is_immutable }
2927  
2928 sub _install_modifier {
2929 my( $self, $type, $name, $code ) = @_;
2930 my $into = $self->name;
2931  
2932 my $original = $into->can($name)
2933 or $self->throw_error("The method '$name' was not found in the inheritance hierarchy for $into");
2934  
2935 my $modifier_table = $self->{modifiers}{$name};
2936  
2937 if(!$modifier_table){
2938 my(@before, @after, @around);
2939 my $cache = $original;
2940 my $modified = sub {
2941 if(@before) {
2942 for my $c (@before) { $c->(@_) }
2943 }
2944 unless(@after) {
2945 return $cache->(@_);
2946 }
2947  
2948 if(wantarray){ # list context
2949 my @rval = $cache->(@_);
2950  
2951 for my $c(@after){ $c->(@_) }
2952 return @rval;
2953 }
2954 elsif(defined wantarray){ # scalar context
2955 my $rval = $cache->(@_);
2956  
2957 for my $c(@after){ $c->(@_) }
2958 return $rval;
2959 }
2960 else{ # void context
2961 $cache->(@_);
2962  
2963 for my $c(@after){ $c->(@_) }
2964 return;
2965 }
2966 };
2967  
2968 $self->{modifiers}{$name} = $modifier_table = {
2969 original => $original,
2970  
2971 before => \@before,
2972 after => \@after,
2973 around => \@around,
2974  
2975 cache => \$cache, # cache for around modifiers
2976 };
2977  
2978 $self->add_method($name => $modified);
2979 }
2980  
2981 if($type eq 'before'){
2982 unshift @{$modifier_table->{before}}, $code;
2983 }
2984 elsif($type eq 'after'){
2985 push @{$modifier_table->{after}}, $code;
2986 }
2987 else{ # around
2988 push @{$modifier_table->{around}}, $code;
2989  
2990 my $next = ${ $modifier_table->{cache} };
2991 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
2992 }
2993  
2994 return;
2995 }
2996  
2997 sub add_before_method_modifier {
2998 my ( $self, $name, $code ) = @_;
2999 $self->_install_modifier( 'before', $name, $code );
3000 }
3001  
3002 sub add_around_method_modifier {
3003 my ( $self, $name, $code ) = @_;
3004 $self->_install_modifier( 'around', $name, $code );
3005 }
3006  
3007 sub add_after_method_modifier {
3008 my ( $self, $name, $code ) = @_;
3009 $self->_install_modifier( 'after', $name, $code );
3010 }
3011  
3012 sub add_override_method_modifier {
3013 my ($self, $name, $code) = @_;
3014  
3015 if($self->has_method($name)){
3016 $self->throw_error("Cannot add an override method if a local method is already present");
3017 }
3018  
3019 my $package = $self->name;
3020  
3021 my $super_body = $package->can($name)
3022 or $self->throw_error("You cannot override '$name' because it has no super method");
3023  
3024 $self->add_method($name => sub {
3025 local $Mousse::SUPER_PACKAGE = $package;
3026 local $Mousse::SUPER_BODY = $super_body;
3027 local @Mousse::SUPER_ARGS = @_;
3028 &{$code};
3029 });
3030 return;
3031 }
3032  
3033 sub add_augment_method_modifier {
3034 my ($self, $name, $code) = @_;
3035 if($self->has_method($name)){
3036 $self->throw_error("Cannot add an augment method if a local method is already present");
3037 }
3038  
3039 my $super = $self->find_method_by_name($name)
3040 or $self->throw_error("You cannot augment '$name' because it has no super method");
3041  
3042 my $super_package = $super->package_name;
3043 my $super_body = $super->body;
3044  
3045 $self->add_method($name => sub {
3046 local $Mousse::INNER_BODY{$super_package} = $code;
3047 local $Mousse::INNER_ARGS{$super_package} = [@_];
3048 &{$super_body};
3049 });
3050 return;
3051 }
3052  
3053 sub does_role {
3054 my ($self, $role_name) = @_;
3055  
3056 (defined $role_name)
3057 || $self->throw_error("You must supply a role name to look for");
3058  
3059 $role_name = $role_name->name if ref $role_name;
3060  
3061 for my $class ($self->linearized_isa) {
3062 my $meta = Mousse::Util::get_metaclass_by_name($class)
3063 or next;
3064  
3065 for my $role (@{ $meta->roles }) {
3066  
3067 return 1 if $role->does_role($role_name);
3068 }
3069 }
3070  
3071 return 0;
3072 }
3073  
3074 # Contents of Mouse::Meta::Method
3075 package Mousse::Meta::Method;
3076 use Mousse::Util qw(:meta); # enables strict and warnings
3077 use Scalar::Util ();
3078  
3079 use overload
3080 '==' => '_equal',
3081 'eq' => '_equal',
3082 '&{}' => sub{ $_[0]->body },
3083 fallback => 1,
3084 ;
3085  
3086 sub wrap {
3087 my $class = shift;
3088 unshift @_, 'body' if @_ % 2 != 0;
3089 return $class->_new(@_);
3090 }
3091  
3092 sub _new{
3093 my($class, %args) = @_;
3094 my $self = bless \%args, $class;
3095  
3096 if($class ne __PACKAGE__){
3097 $self->meta->_initialize_object($self, \%args);
3098 }
3099 return $self;
3100 }
3101  
3102 sub body { $_[0]->{body} }
3103 sub name { $_[0]->{name} }
3104 sub package_name { $_[0]->{package} }
3105 sub associated_metaclass { $_[0]->{associated_metaclass} }
3106  
3107 sub fully_qualified_name {
3108 my($self) = @_;
3109 return $self->package_name . '::' . $self->name;
3110 }
3111  
3112 # for Moose compat
3113 sub _equal {
3114 my($l, $r) = @_;
3115  
3116 return Scalar::Util::blessed($r)
3117 && $l->body == $r->body
3118 && $l->name eq $r->name
3119 && $l->package_name eq $r->package_name;
3120 }
3121  
3122 # Contents of Mouse::Meta::Method::Accessor
3123 package Mousse::Meta::Method::Accessor;
3124 use Mousse::Util qw(:meta); # enables strict and warnings
3125  
3126 use constant _MOUSE_DEBUG => $ENV{MOUSE_DEBUG} ? 1 : 0;
3127  
3128 sub _inline_slot{
3129 my(undef, $self_var, $attr_name) = @_;
3130 return sprintf '%s->{q{%s}}', $self_var, $attr_name;
3131 }
3132  
3133 sub _generate_accessor_any{
3134 my($method_class, $type, $attribute, $class) = @_;
3135  
3136 my $name = $attribute->name;
3137 my $default = $attribute->default;
3138 my $constraint = $attribute->type_constraint;
3139 my $builder = $attribute->builder;
3140 my $trigger = $attribute->trigger;
3141 my $is_weak = $attribute->is_weak_ref;
3142 my $should_deref = $attribute->should_auto_deref;
3143 my $should_coerce = (defined($constraint)
3144 && $constraint->has_coercion
3145 && $attribute->should_coerce);
3146  
3147 my $compiled_type_constraint = defined($constraint)
3148 ? $constraint->_compiled_type_constraint
3149 : undef;
3150  
3151 my $self = '$_[0]';
3152 my $slot = $method_class->_inline_slot($self, $name);;
3153  
3154 my $accessor = sprintf(qq{package %s;\n#line 1 "%s-accessor for %s (%s)"\n}, $class->name, $type, $name, __FILE__)
3155 . "sub {\n";
3156  
3157 if ($type eq 'rw' || $type eq 'wo') {
3158 if($type eq 'rw'){
3159 $accessor .=
3160 'if (scalar(@_) >= 2) {' . "\n";
3161 }
3162 else{ # writer
3163 $accessor .=
3164 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of $name") }'.
3165 '{' . "\n";
3166 }
3167  
3168 my $value = '$_[1]';
3169  
3170 if (defined $constraint) {
3171 if ($should_coerce) {
3172 $accessor .=
3173 "\n".
3174 'my $val = $constraint->coerce('.$value.');';
3175 $value = '$val';
3176 }
3177 $accessor .=
3178 "\n".
3179 '$compiled_type_constraint->('.$value.') or
3180 $attribute->_throw_type_constraint_error('.$value.', $constraint);' . "\n";
3181 }
3182  
3183 # if there's nothing left to do for the attribute we can return during
3184 # this setter
3185 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
3186  
3187 $accessor .= "$slot = $value;\n";
3188  
3189 if ($is_weak) {
3190 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
3191 }
3192  
3193 if ($trigger) {
3194 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
3195 }
3196  
3197 $accessor .= "}\n";
3198 }
3199 elsif($type eq 'ro') {
3200 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor of $name") if scalar(@_) >= 2;' . "\n";
3201 }
3202 else{
3203 $class->throw_error("Unknown accessor type '$type'");
3204 }
3205  
3206 if ($attribute->is_lazy and $type ne 'wo') {
3207 my $value;
3208  
3209 if (defined $builder){
3210 $value = "$self->\$builder()";
3211 }
3212 elsif (ref($default) eq 'CODE'){
3213 $value = "$self->\$default()";
3214 }
3215 else{
3216 $value = '$default';
3217 }
3218  
3219 $accessor .= "els" if $type eq 'rw';
3220 $accessor .= "if(!exists $slot){\n";
3221 if($should_coerce){
3222 $accessor .= "$slot = \$constraint->coerce($value)";
3223 }
3224 elsif(defined $constraint){
3225 $accessor .= "my \$tmp = $value;\n";
3226 $accessor .= "\$compiled_type_constraint->(\$tmp)";
3227 $accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n";
3228 $accessor .= "$slot = \$tmp;\n";
3229 }
3230 else{
3231 $accessor .= "$slot = $value;\n";
3232 }
3233 if ($is_weak) {
3234 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
3235 }
3236 $accessor .= "}\n";
3237 }
3238  
3239 if ($should_deref) {
3240 if ($constraint->is_a_type_of('ArrayRef')) {
3241 $accessor .= "return \@{ $slot || [] } if wantarray;\n";
3242 }
3243 elsif($constraint->is_a_type_of('HashRef')){
3244 $accessor .= "return \%{ $slot || {} } if wantarray;\n";
3245 }
3246 else{
3247 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
3248 }
3249 }
3250  
3251 $accessor .= "return $slot;\n}\n";
3252  
3253 warn $accessor if _MOUSE_DEBUG;
3254 my $code;
3255 my $e = do{
3256 local $@;
3257 $code = eval $accessor;
3258 $@;
3259 };
3260 die $e if $e;
3261  
3262 return $code;
3263 }
3264  
3265 sub _generate_accessor{
3266 #my($self, $attribute, $metaclass) = @_;
3267 my $self = shift;
3268 return $self->_generate_accessor_any(rw => @_);
3269 }
3270  
3271 sub _generate_reader {
3272 #my($self, $attribute, $metaclass) = @_;
3273 my $self = shift;
3274 return $self->_generate_accessor_any(ro => @_);
3275 }
3276  
3277 sub _generate_writer {
3278 #my($self, $attribute, $metaclass) = @_;
3279 my $self = shift;
3280 return $self->_generate_accessor_any(wo => @_);
3281 }
3282  
3283 sub _generate_predicate {
3284 #my($self, $attribute, $metaclass) = @_;
3285 my(undef, $attribute) = @_;
3286  
3287 my $slot = $attribute->name;
3288 return sub{
3289 return exists $_[0]->{$slot};
3290 };
3291 }
3292  
3293 sub _generate_clearer {
3294 #my($self, $attribute, $metaclass) = @_;
3295 my(undef, $attribute) = @_;
3296  
3297 my $slot = $attribute->name;
3298 return sub{
3299 delete $_[0]->{$slot};
3300 };
3301 }
3302  
3303 # Contents of Mouse::Meta::Method::Constructor
3304 package Mousse::Meta::Method::Constructor;
3305 use Mousse::Util qw(:meta); # enables strict and warnings
3306  
3307 use constant _MOUSE_DEBUG => $ENV{MOUSE_DEBUG} ? 1 : 0;
3308  
3309 sub _inline_slot{
3310 my(undef, $self_var, $attr_name) = @_;
3311 return sprintf '%s->{q{%s}}', $self_var, $attr_name;
3312 }
3313  
3314 sub _generate_constructor {
3315 my ($class, $metaclass, $args) = @_;
3316  
3317 my $associated_metaclass_name = $metaclass->name;
3318  
3319 my $buildall = $class->_generate_BUILDALL($metaclass);
3320 my $buildargs = $class->_generate_BUILDARGS($metaclass);
3321 my $initializer = $metaclass->{_mouse_cache}{_initialize_object} ||=
3322 $class->_generate_initialize_object($metaclass);
3323 my $source = sprintf(<<'EOT', __FILE__, $metaclass->name, $buildargs, $buildall);
3324 #line 1 "%s"
3325 package %s;
3326 sub {
3327 my $class = shift;
3328 return $class->Mousse::Object::new(@_)
3329 if $class ne __PACKAGE__;
3330 # BUILDARGS
3331 %s;
3332 my $instance = bless {}, $class;
3333 $metaclass->$initializer($instance, $args, 0);
3334 # BUILDALL
3335 %s;
3336 return $instance;
3337 }
3338 EOT
3339 warn $source if _MOUSE_DEBUG;
3340 my $body;
3341 my $e = do{
3342 local $@;
3343 $body = eval $source;
3344 $@;
3345 };
3346 die $e if $e;
3347 return $body;
3348 }
3349  
3350 sub _generate_initialize_object {
3351 my ($method_class, $metaclass) = @_;
3352 my @attrs = $metaclass->get_all_attributes;
3353  
3354 my @checks = map { $_ && $_->_compiled_type_constraint }
3355 map { $_->type_constraint } @attrs;
3356  
3357 my @res;
3358  
3359 my $has_triggers;
3360 my $strict = $metaclass->strict_constructor;
3361  
3362 if($strict){
3363 push @res, 'my $used = 0;';
3364 }
3365  
3366 for my $index (0 .. @attrs - 1) {
3367 my $code = '';
3368  
3369 my $attr = $attrs[$index];
3370 my $key = $attr->name;
3371  
3372 my $init_arg = $attr->init_arg;
3373 my $type_constraint = $attr->type_constraint;
3374 my $is_weak_ref = $attr->is_weak_ref;
3375 my $need_coercion;
3376  
3377 my $instance_slot = $method_class->_inline_slot('$instance', $key);
3378 my $attr_var = "\$attrs[$index]";
3379 my $constraint_var;
3380  
3381 if(defined $type_constraint){
3382 $constraint_var = "$attr_var\->{type_constraint}";
3383 $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion);
3384 }
3385  
3386 $code .= "# initialize $key\n";
3387  
3388 my $post_process = '';
3389 if(defined $type_constraint){
3390 $post_process .= "\$checks[$index]->($instance_slot)\n";
3391 $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
3392 }
3393  
3394 # build cde for an attribute
3395 if (defined $init_arg) {
3396 my $value = "\$args->{q{$init_arg}}";
3397  
3398 $code .= "if (exists $value) {\n";
3399  
3400 if($need_coercion){
3401 $value = "$constraint_var->coerce($value)";
3402 }
3403  
3404 $code .= "$instance_slot = $value;\n";
3405 $code .= $post_process;
3406  
3407 if ($attr->has_trigger) {
3408 $has_triggers++;
3409 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
3410 }
3411  
3412 if ($strict){
3413 $code .= '++$used;' . "\n";
3414 }
3415  
3416 $code .= "\n} else {\n"; # $value exists
3417 }
3418  
3419 if ($attr->has_default || $attr->has_builder) {
3420 unless ($attr->is_lazy) {
3421 my $default = $attr->default;
3422 my $builder = $attr->builder;
3423  
3424 my $value;
3425 if (defined($builder)) {
3426 $value = "\$instance->$builder()";
3427 }
3428 elsif (ref($default) eq 'CODE') {
3429 $value = "$attr_var\->{default}->(\$instance)";
3430 }
3431 elsif (defined($default)) {
3432 $value = "$attr_var\->{default}";
3433 }
3434 else {
3435 $value = 'undef';
3436 }
3437  
3438 if($need_coercion){
3439 $value = "$constraint_var->coerce($value)";
3440 }
3441  
3442 $code .= "$instance_slot = $value;\n";
3443 $code .= $post_process;
3444 }
3445 }
3446 elsif ($attr->is_required) {
3447 $code .= "\$meta->throw_error('Attribute ($key) is required')";
3448 $code .= " unless \$is_cloning;\n";
3449 }
3450  
3451 $code .= "}\n" if defined $init_arg;
3452  
3453 if($is_weak_ref){
3454 $code .= "Scalar::Util::weaken($instance_slot) "
3455 . "if ref $instance_slot;\n";
3456 }
3457  
3458 push @res, $code;
3459 }
3460  
3461 if($strict){
3462 push @res, q{if($used < keys %{$args})}
3463 . q{{ $meta->_report_unknown_args(\@attrs, $args) }};
3464 }
3465  
3466 if($metaclass->is_anon_class){
3467 push @res, q{$instance->{__METACLASS__} = $meta;};
3468 }
3469  
3470 if($has_triggers){
3471 unshift @res, q{my @triggers;};
3472 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
3473 }
3474  
3475 my $source = sprintf <<'EOT', __FILE__, $metaclass->name, join "\n", @res;
3476 #line 1 "%s"
3477 package %s;
3478 sub {
3479 my($meta, $instance, $args, $is_cloning) = @_;
3480 %s;
3481 return $instance;
3482 }
3483 EOT
3484 warn $source if _MOUSE_DEBUG;
3485 my $body;
3486 my $e = do {
3487 local $@;
3488 $body = eval $source;
3489 $@;
3490 };
3491 die $e if $e;
3492 return $body;
3493 }
3494  
3495 sub _generate_BUILDARGS {
3496 my(undef, $metaclass) = @_;
3497  
3498 my $class = $metaclass->name;
3499 if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mousse::Object::BUILDARGS ) {
3500 return 'my $args = $class->BUILDARGS(@_)';
3501 }
3502  
3503 return <<'...';
3504 my $args;
3505 if ( scalar @_ == 1 ) {
3506 ( ref( $_[0] ) eq 'HASH' )
3507 || Carp::confess "Single parameters to new() must be a HASH ref";
3508 $args = +{ %{ $_[0] } };
3509 }
3510 else {
3511 $args = +{@_};
3512 }
3513 ...
3514 }
3515  
3516 sub _generate_BUILDALL {
3517 my (undef, $metaclass) = @_;
3518  
3519 return '' unless $metaclass->name->can('BUILD');
3520  
3521 my @code;
3522 for my $class ($metaclass->linearized_isa) {
3523 if (Mousse::Util::get_code_ref($class, 'BUILD')) {
3524 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
3525 }
3526 }
3527 return join "\n", @code;
3528 }
3529  
3530 # Contents of Mouse::Meta::Method::Delegation
3531 package Mousse::Meta::Method::Delegation;
3532 use Mousse::Util qw(:meta); # enables strict and warnings
3533 use Scalar::Util;
3534  
3535 sub _generate_delegation{
3536 my (undef, $attr, $handle_name, $method_to_call) = @_;
3537  
3538 my @curried_args;
3539 if(ref($method_to_call) eq 'ARRAY'){
3540 ($method_to_call, @curried_args) = @{$method_to_call};
3541 }
3542  
3543 # If it has a reader, we must use it to make method modifiers work
3544 my $reader = $attr->get_read_method() || $attr->get_read_method_ref();
3545  
3546 my $can_be_optimized = $attr->{_mouse_cache_method_delegation_can_be_optimized};
3547  
3548 if(!defined $can_be_optimized){
3549 my $tc = $attr->type_constraint;
3550 $attr->{_mouse_cache_method_delegation_can_be_optimized} =
3551 (defined($tc) && $tc->is_a_type_of('Object'))
3552 && ($attr->is_required || $attr->has_default || $attr->has_builder)
3553 && ($attr->is_lazy || !$attr->has_clearer);
3554 }
3555  
3556 if($can_be_optimized){
3557 # need not check the attribute value
3558 return sub {
3559 return shift()->$reader()->$method_to_call(@curried_args, @_);
3560 };
3561 }
3562 else {
3563 # need to check the attribute value
3564 return sub {
3565 my $instance = shift;
3566 my $proxy = $instance->$reader();
3567  
3568 my $error = !defined($proxy) ? ' is not defined'
3569 : ref($proxy) && !Scalar::Util::blessed($proxy) ? qq{ is not an object (got '$proxy')}
3570 : undef;
3571 if ($error) {
3572 $instance->meta->throw_error(
3573 "Cannot delegate $handle_name to $method_to_call because "
3574 . "the value of "
3575 . $attr->name
3576 . $error
3577 );
3578 }
3579 $proxy->$method_to_call(@curried_args, @_);
3580 };
3581 }
3582 }
3583  
3584  
3585 # Contents of Mouse::Meta::Method::Destructor
3586 package Mousse::Meta::Method::Destructor;
3587 use Mousse::Util qw(:meta); # enables strict and warnings
3588  
3589 use constant _MOUSE_DEBUG => $ENV{MOUSE_DEBUG} ? 1 : 0;
3590  
3591 sub _generate_destructor{
3592 my (undef, $metaclass) = @_;
3593  
3594 my $demolishall = '';
3595 for my $class ($metaclass->linearized_isa) {
3596 if (Mousse::Util::get_code_ref($class, 'DEMOLISH')) {
3597 $demolishall .= ' ' . $class
3598 . '::DEMOLISH($self, $Mousse::Util::in_global_destruction);'
3599 . "\n",
3600 }
3601 }
3602  
3603 if($demolishall) {
3604 $demolishall = sprintf <<'EOT', $demolishall;
3605 my $e = do{
3606 local $?;
3607 local $@;
3608 eval{
3609 %s;
3610 };
3611 $@;
3612 };
3613 no warnings 'misc';
3614 die $e if $e; # rethrow
3615 EOT
3616 }
3617  
3618 my $name = $metaclass->name;
3619 my $source = sprintf(<<'EOT', __FILE__, $name, $demolishall);
3620 #line 1 "%s"
3621 package %s;
3622 sub {
3623 my($self) = @_;
3624 return $self->Mousse::Object::DESTROY()
3625 if ref($self) ne __PACKAGE__;
3626 # DEMOLISHALL
3627 %s;
3628 return;
3629 }
3630 EOT
3631  
3632 warn $source if _MOUSE_DEBUG;
3633  
3634 my $code;
3635 my $e = do{
3636 local $@;
3637 $code = eval $source;
3638 $@;
3639 };
3640 die $e if $e;
3641 return $code;
3642 }
3643  
3644 # Contents of Mouse::Meta::Module
3645 package Mousse::Meta::Module;
3646 use Mousse::Util qw/:meta/; # enables strict and warnings
3647 no warnings 'once';
3648  
3649 use Carp ();
3650 use Scalar::Util ();
3651  
3652 my %METAS;
3653  
3654 if(Mousse::Util::MOUSE_XS){
3655 # register meta storage for performance
3656 Mousse::Util::__register_metaclass_storage(\%METAS, 0);
3657  
3658 # ensure thread safety
3659 *CLONE = sub { Mousse::Util::__register_metaclass_storage(\%METAS, 1) };
3660 }
3661  
3662 sub initialize {
3663 my($class, $package_name, @args) = @_;
3664  
3665 ($package_name && !ref($package_name))
3666 || $class->throw_error("You must pass a package name and it cannot be blessed");
3667  
3668 return $METAS{$package_name}
3669 ||= $class->_construct_meta(package => $package_name, @args);
3670 }
3671  
3672 sub reinitialize {
3673 my($class, $package_name, @args) = @_;
3674  
3675 $package_name = $package_name->name if ref $package_name;
3676  
3677 ($package_name && !ref($package_name))
3678 || $class->throw_error("You must pass a package name and it cannot be blessed");
3679  
3680 if(exists $METAS{$package_name}) {
3681 unshift @args, %{ $METAS{$package_name} };
3682 }
3683 delete $METAS{$package_name};
3684 return $class->initialize($package_name, @args);
3685 }
3686  
3687 sub _class_of{
3688 my($class_or_instance) = @_;
3689 return undef unless defined $class_or_instance;
3690 return $METAS{ ref($class_or_instance) || $class_or_instance };
3691 }
3692  
3693 # Means of accessing all the metaclasses that have
3694 # been initialized thus far.
3695 # The public versions are aliased into Mousse::Util::*.
3696 #sub _get_all_metaclasses { %METAS }
3697 sub _get_all_metaclass_instances { values %METAS }
3698 sub _get_all_metaclass_names { keys %METAS }
3699 sub _get_metaclass_by_name { $METAS{$_[0]} }
3700 #sub _store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
3701 #sub _weaken_metaclass { weaken($METAS{$_[0]}) }
3702 #sub _does_metaclass_exist { defined $METAS{$_[0]} }
3703 #sub _remove_metaclass_by_name { delete $METAS{$_[0]} }
3704  
3705 sub name;
3706  
3707 sub namespace;
3708  
3709 # add_attribute is an abstract method
3710  
3711 sub get_attribute_map { # DEPRECATED
3712 Carp::cluck('get_attribute_map() has been deprecated. Use get_attribute_list() and get_attribute() instead');
3713 return $_[0]->{attributes};
3714 }
3715  
3716 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
3717 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
3718 sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
3719  
3720 sub get_attribute_list{ keys %{$_[0]->{attributes}} }
3721  
3722 # XXX: not completely compatible with Moose
3723 my %foreign = map{ $_ => undef } qw(
3724 Mousse Mousse::Role Mousse::Util Mousse::Util::TypeConstraints
3725 Carp Scalar::Util List::Util
3726 );
3727 sub _get_method_body {
3728 my($self, $method_name) = @_;
3729 my $code = Mousse::Util::get_code_ref($self->{package}, $method_name);
3730 return $code && !exists $foreign{ Mousse::Util::get_code_package($code) }
3731 ? $code
3732 : undef;
3733 }
3734  
3735 sub add_method;
3736  
3737 sub has_method {
3738 my($self, $method_name) = @_;
3739 defined($method_name)
3740 or $self->throw_error('You must define a method name');
3741  
3742 return defined( $self->{methods}{$method_name} )
3743 || defined( $self->_get_method_body($method_name) );
3744 }
3745  
3746 sub get_method_body {
3747 my($self, $method_name) = @_;
3748 defined($method_name)
3749 or $self->throw_error('You must define a method name');
3750  
3751 return $self->{methods}{$method_name}
3752 ||= $self->_get_method_body($method_name);
3753 }
3754  
3755 sub get_method {
3756 my($self, $method_name) = @_;
3757  
3758 if(my $code = $self->get_method_body($method_name)){
3759 return Mousse::Util::load_class($self->method_metaclass)->wrap(
3760 body => $code,
3761 name => $method_name,
3762 package => $self->name,
3763 associated_metaclass => $self,
3764 );
3765 }
3766  
3767 return undef;
3768 }
3769  
3770 sub get_method_list {
3771 my($self) = @_;
3772  
3773 return grep { $self->has_method($_) } keys %{ $self->namespace };
3774 }
3775  
3776 sub _collect_methods { # Mousse specific, used for method modifiers
3777 my($meta, @args) = @_;
3778  
3779 my @methods;
3780 foreach my $arg(@args){
3781 if(my $type = ref $arg){
3782 if($type eq 'Regexp'){
3783 push @methods, grep { $_ =~ $arg } $meta->get_all_method_names;
3784 }
3785 elsif($type eq 'ARRAY'){
3786 push @methods, @{$arg};
3787 }
3788 else{
3789 my $subname = ( caller(1) )[3];
3790 $meta->throw_error(
3791 sprintf(
3792 'Methods passed to %s must be provided as a list,'
3793 . ' ArrayRef or regular expression, not %s',
3794 $subname,
3795 $type,
3796 )
3797 );
3798 }
3799 }
3800 else{
3801 push @methods, $arg;
3802 }
3803 }
3804 return @methods;
3805 }
3806  
3807 my $ANON_SERIAL = 0; # anonymous class/role id
3808 my %IMMORTALS; # immortal anonymous classes
3809  
3810 sub create {
3811 my($self, $package_name, %options) = @_;
3812  
3813 my $class = ref($self) || $self;
3814 $self->throw_error('You must pass a package name') if @_ < 2;
3815  
3816 my $superclasses;
3817 if(exists $options{superclasses}){
3818 if(Mousse::Util::is_a_metarole($self)){
3819 delete $options{superclasses};
3820 }
3821 else{
3822 $superclasses = delete $options{superclasses};
3823 (ref $superclasses eq 'ARRAY')
3824 || $self->throw_error("You must pass an ARRAY ref of superclasses");
3825 }
3826 }
3827  
3828 my $attributes = delete $options{attributes};
3829 if(defined $attributes){
3830 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
3831 || $self->throw_error("You must pass an ARRAY ref of attributes");
3832 }
3833 my $methods = delete $options{methods};
3834 if(defined $methods){
3835 (ref $methods eq 'HASH')
3836 || $self->throw_error("You must pass a HASH ref of methods");
3837 }
3838 my $roles = delete $options{roles};
3839 if(defined $roles){
3840 (ref $roles eq 'ARRAY')
3841 || $self->throw_error("You must pass an ARRAY ref of roles");
3842 }
3843 my $mortal;
3844 my $cache_key;
3845  
3846 if(!defined $package_name){ # anonymous
3847 $mortal = !$options{cache};
3848  
3849 # anonymous but immortal
3850 if(!$mortal){
3851 # something like Super::Class|Super::Class::2=Role|Role::1
3852 $cache_key = join '=' => (
3853 join('|', @{$superclasses || []}),
3854 join('|', sort @{$roles || []}),
3855 );
3856 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
3857 }
3858 $options{anon_serial_id} = ++$ANON_SERIAL;
3859 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
3860 }
3861  
3862  
3863 # instantiate a module
3864 {
3865 no strict 'refs';
3866 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
3867 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
3868 }
3869  
3870 my $meta = $self->initialize( $package_name, %options);
3871  
3872 Scalar::Util::weaken($METAS{$package_name})
3873 if $mortal;
3874  
3875 $meta->add_method(meta => sub {
3876 $self->initialize(ref($_[0]) || $_[0]);
3877 });
3878  
3879 $meta->superclasses(@{$superclasses})
3880 if defined $superclasses;
3881  
3882 # NOTE:
3883 # process attributes first, so that they can
3884 # install accessors, but locally defined methods
3885 # can then overwrite them. It is maybe a little odd, but
3886 # I think this should be the order of things.
3887 if (defined $attributes) {
3888 if(ref($attributes) eq 'ARRAY'){
3889 # array of Mousse::Meta::Attribute
3890 foreach my $attr (@{$attributes}) {
3891 $meta->add_attribute($attr);
3892 }
3893 }
3894 else{
3895 # hash map of name and attribute spec pairs
3896 while(my($name, $attr) = each %{$attributes}){
3897 $meta->add_attribute($name => $attr);
3898 }
3899 }
3900 }
3901 if (defined $methods) {
3902 while(my($method_name, $method_body) = each %{$methods}){
3903 $meta->add_method($method_name, $method_body);
3904 }
3905 }
3906 if (defined $roles and !$options{in_application_to_instance}){
3907 Mousse::Util::apply_all_roles($package_name, @{$roles});
3908 }
3909  
3910 if($cache_key){
3911 $IMMORTALS{$cache_key} = $meta;
3912 }
3913  
3914 return $meta;
3915 }
3916  
3917 sub DESTROY{
3918 my($self) = @_;
3919  
3920 return if $Mousse::Util::in_global_destruction;
3921  
3922 my $serial_id = $self->{anon_serial_id};
3923 return if !$serial_id;
3924  
3925 # XXX: cleaning stash with threads causes panic/SEGV on legacy perls.
3926 if(exists $INC{'threads.pm'}) {
3927 # (caller)[2] indicates the caller's line number,
3928 # which is zero when the current thread is joining (destroying).
3929 return if( (caller)[2] == 0);
3930 }
3931  
3932 # clean up mortal anonymous class stuff
3933  
3934 # @ISA is a magical variable, so we must clear it manually.
3935 @{$self->{superclasses}} = () if exists $self->{superclasses};
3936  
3937 # Then, clear the symbol table hash
3938 %{$self->namespace} = ();
3939  
3940 my $name = $self->name;
3941 delete $METAS{$name};
3942  
3943 $name =~ s/ $serial_id \z//xms;
3944 no strict 'refs';
3945 delete ${$name}{ $serial_id . '::' };
3946 return;
3947 }
3948  
3949  
3950 # Contents of Mouse::Meta::Role
3951 package Mousse::Meta::Role;
3952 use Mousse::Util qw(:meta); # enables strict and warnings
3953  
3954 use Mousse::Meta::Module;
3955 our @ISA = qw(Mousse::Meta::Module);
3956  
3957 sub method_metaclass;
3958  
3959 sub _construct_meta {
3960 my $class = shift;
3961  
3962 my %args = @_;
3963  
3964 $args{methods} = {};
3965 $args{attributes} = {};
3966 $args{required_methods} = [];
3967 $args{roles} = [];
3968  
3969 my $self = bless \%args, ref($class) || $class;
3970 if($class ne __PACKAGE__){
3971 $self->meta->_initialize_object($self, \%args);
3972 }
3973 return $self;
3974 }
3975  
3976 sub create_anon_role{
3977 my $self = shift;
3978 return $self->create(undef, @_);
3979 }
3980  
3981 sub is_anon_role;
3982  
3983 sub get_roles;
3984  
3985 sub calculate_all_roles {
3986 my $self = shift;
3987 my %seen;
3988 return grep { !$seen{ $_->name }++ }
3989 ($self, map { $_->calculate_all_roles } @{ $self->get_roles });
3990 }
3991  
3992 sub get_required_method_list{
3993 return @{ $_[0]->{required_methods} };
3994 }
3995  
3996 sub add_required_methods {
3997 my($self, @methods) = @_;
3998 my %required = map{ $_ => 1 } @{$self->{required_methods}};
3999 push @{$self->{required_methods}}, grep{ !$required{$_}++ && !$self->has_method($_) } @methods;
4000 return;
4001 }
4002  
4003 sub requires_method {
4004 my($self, $name) = @_;
4005 return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0;
4006 }
4007  
4008 sub add_attribute {
4009 my $self = shift;
4010 my $name = shift;
4011  
4012 $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
4013 return;
4014 }
4015  
4016 sub apply {
4017 my $self = shift;
4018 my $consumer = shift;
4019  
4020 require 'Mousse/Meta/Role/Application.pm';
4021 return Mousse::Meta::Role::Application->new(@_)->apply($self, $consumer);
4022 }
4023  
4024 sub combine {
4025 my($self, @role_specs) = @_;
4026  
4027 require 'Mousse/Meta/Role/Composite.pm';
4028 return Mousse::Meta::Role::Composite->new(roles => \@role_specs);
4029 }
4030  
4031 sub add_before_method_modifier;
4032 sub add_around_method_modifier;
4033 sub add_after_method_modifier;
4034  
4035 sub get_before_method_modifiers;
4036 sub get_around_method_modifiers;
4037 sub get_after_method_modifiers;
4038  
4039 sub add_override_method_modifier{
4040 my($self, $method_name, $method) = @_;
4041  
4042 if($self->has_method($method_name)){
4043 # This error happens in the override keyword or during role composition,
4044 # so I added a message, "A local method of ...", only for compatibility (gfx)
4045 $self->throw_error("Cannot add an override of method '$method_name' "
4046 . "because there is a local version of '$method_name'"
4047 . "(A local method of the same name as been found)");
4048 }
4049  
4050 $self->{override_method_modifiers}->{$method_name} = $method;
4051 }
4052  
4053 sub get_override_method_modifier {
4054 my ($self, $method_name) = @_;
4055 return $self->{override_method_modifiers}->{$method_name};
4056 }
4057  
4058 sub does_role {
4059 my ($self, $role_name) = @_;
4060  
4061 (defined $role_name)
4062 || $self->throw_error("You must supply a role name to look for");
4063  
4064 $role_name = $role_name->name if ref $role_name;
4065  
4066 # if we are it,.. then return true
4067 return 1 if $role_name eq $self->name;
4068 # otherwise.. check our children
4069 for my $role (@{ $self->get_roles }) {
4070 return 1 if $role->does_role($role_name);
4071 }
4072 return 0;
4073 }
4074  
4075 # Contents of Mouse::Meta::Role::Application
4076 package Mousse::Meta::Role::Application;
4077 use Mousse::Util qw(:meta);
4078  
4079 sub new {
4080 my $class = shift;
4081 my $args = $class->Mousse::Object::BUILDARGS(@_);
4082  
4083 if(exists $args->{exclude} or exists $args->{alias}) {
4084 warnings::warnif(deprecated =>
4085 'The alias and excludes options for role application have been'
4086 . ' renamed -alias and -exclude');
4087  
4088 if($args->{alias} && !exists $args->{-alias}){
4089 $args->{-alias} = $args->{alias};
4090 }
4091 if($args->{excludes} && !exists $args->{-excludes}){
4092 $args->{-excludes} = $args->{excludes};
4093 }
4094 }
4095  
4096 $args->{aliased_methods} = {};
4097 if(my $alias = $args->{-alias}){
4098 @{$args->{aliased_methods}}{ values %{$alias} } = ();
4099 }
4100  
4101 if(my $excludes = $args->{-excludes}){
4102 $args->{-excludes} = {}; # replace with a hash ref
4103 if(ref $excludes){
4104 %{$args->{-excludes}} = (map{ $_ => undef } @{$excludes});
4105 }
4106 else{
4107 $args->{-excludes}{$excludes} = undef;
4108 }
4109 }
4110 my $self = bless $args, $class;
4111 if($class ne __PACKAGE__){
4112 $self->meta->_initialize_object($self, $args);
4113 }
4114 return $self;
4115 }
4116  
4117 sub apply {
4118 my($self, $role, $consumer, @extra) = @_;
4119 my $instance;
4120  
4121 if(Mousse::Util::is_a_metaclass($consumer)) { # Application::ToClass
4122 $self->{_to} = 'class';
4123 }
4124 elsif(Mousse::Util::is_a_metarole($consumer)) { # Application::ToRole
4125 $self->{_to} = 'role';
4126 }
4127 else { # Appplication::ToInstance
4128 $self->{_to} = 'instance';
4129 $instance = $consumer;
4130  
4131 $consumer = (Mousse::Util::class_of($instance) || 'Mousse::Meta::Class')
4132 ->create_anon_class(
4133 superclasses => [ref $instance],
4134 roles => [$role],
4135 cache => 1,
4136  
4137 in_application_to_instance => 1, # suppress to apply roles
4138 );
4139 }
4140  
4141 #$self->check_role_exclusions($role, $consumer, @extra);
4142 $self->check_required_methods($role, $consumer, @extra);
4143 #$self->check_required_attributes($role, $consumer, @extra);
4144  
4145 $self->apply_attributes($role, $consumer, @extra);
4146 $self->apply_methods($role, $consumer, @extra);
4147 #$self->apply_override_method_modifiers($role, $consumer, @extra);
4148 #$self->apply_before_method_modifiers($role, $consumer, @extra);
4149 #$self->apply_around_method_modifiers($role, $consumer, @extra);
4150 #$self->apply_after_method_modifiers($role, $consumer, @extra);
4151 $self->apply_modifiers($role, $consumer, @extra);
4152  
4153 $self->_append_roles($role, $consumer);
4154  
4155 if(defined $instance){ # Application::ToInstance
4156 # rebless instance
4157 bless $instance, $consumer->name;
4158 $consumer->_initialize_object($instance, $instance, 1);
4159 }
4160  
4161 return;
4162 }
4163  
4164 sub check_required_methods {
4165 my($self, $role, $consumer) = @_;
4166  
4167 if($self->{_to} eq 'role'){
4168 $consumer->add_required_methods($role->get_required_method_list);
4169 }
4170 else{ # to class or instance
4171 my $consumer_class_name = $consumer->name;
4172  
4173 my @missing;
4174 foreach my $method_name(@{$role->{required_methods}}){
4175 next if exists $self->{aliased_methods}{$method_name};
4176 next if exists $role->{methods}{$method_name};
4177 next if $consumer_class_name->can($method_name);
4178  
4179 push @missing, $method_name;
4180 }
4181 if(@missing){
4182 $role->throw_error(sprintf "'%s' requires the method%s %s to be implemented by '%s'",
4183 $role->name,
4184 (@missing == 1 ? '' : 's'), # method or methods
4185 Mousse::Util::quoted_english_list(@missing),
4186 $consumer_class_name);
4187 }
4188 }
4189  
4190 return;
4191 }
4192  
4193 sub apply_methods {
4194 my($self, $role, $consumer) = @_;
4195  
4196 my $alias = $self->{-alias};
4197 my $excludes = $self->{-excludes};
4198  
4199 foreach my $method_name($role->get_method_list){
4200 next if $method_name eq 'meta';
4201  
4202 my $code = $role->get_method_body($method_name);
4203  
4204 if(!exists $excludes->{$method_name}){
4205 if(!$consumer->has_method($method_name)){
4206 # The third argument $role is used in Role::Composite
4207 $consumer->add_method($method_name => $code, $role);
4208 }
4209 }
4210  
4211 if(exists $alias->{$method_name}){
4212 my $dstname = $alias->{$method_name};
4213  
4214 my $dstcode = $consumer->get_method_body($dstname);
4215  
4216 if(defined($dstcode) && $dstcode != $code){
4217 $role->throw_error("Cannot create a method alias if a local method of the same name exists");
4218 }
4219 else{
4220 $consumer->add_method($dstname => $code, $role);
4221 }
4222 }
4223 }
4224  
4225 return;
4226 }
4227  
4228 sub apply_attributes {
4229 my($self, $role, $consumer) = @_;
4230  
4231 for my $attr_name ($role->get_attribute_list) {
4232 next if $consumer->has_attribute($attr_name);
4233  
4234 $consumer->add_attribute($attr_name
4235 => $role->get_attribute($attr_name));
4236 }
4237 return;
4238 }
4239  
4240 sub apply_modifiers {
4241 my($self, $role, $consumer) = @_;
4242  
4243 if(my $modifiers = $role->{override_method_modifiers}){
4244 foreach my $method_name (keys %{$modifiers}){
4245 $consumer->add_override_method_modifier(
4246 $method_name => $modifiers->{$method_name});
4247 }
4248 }
4249  
4250 for my $modifier_type (qw/before around after/) {
4251 my $table = $role->{"${modifier_type}_method_modifiers"}
4252 or next;
4253  
4254 my $add_modifier = "add_${modifier_type}_method_modifier";
4255  
4256 while(my($method_name, $modifiers) = each %{$table}){
4257 foreach my $code(@{ $modifiers }) {
4258 # skip if the modifier is already applied
4259 next if $consumer->{"_applied_$modifier_type"}{$method_name, $code}++;
4260 $consumer->$add_modifier($method_name => $code);
4261 }
4262 }
4263 }
4264 return;
4265 }
4266  
4267 sub _append_roles {
4268 my($self, $role, $metaclass_or_role) = @_;
4269  
4270 my $roles = $metaclass_or_role->{roles};
4271 foreach my $r($role, @{$role->get_roles}){
4272 if(!$metaclass_or_role->does_role($r)){
4273 push @{$roles}, $r;
4274 }
4275 }
4276 return;
4277 }
4278 # Contents of Mouse::Meta::Role::Composite
4279 package Mousse::Meta::Role::Composite;
4280 use Mousse::Util; # enables strict and warnings
4281 use Mousse::Meta::Role;
4282 use Mousse::Meta::Role::Application;
4283 our @ISA = qw(Mousse::Meta::Role);
4284  
4285 # FIXME: Mousse::Meta::Role::Composite does things in different way from Moose's
4286 # Moose: creates a new class for the consumer, and applies roles to it.
4287 # Mousse: creates a coposite role and apply roles to the role,
4288 # and then applies it to the consumer.
4289  
4290 sub new {
4291 my $class = shift;
4292 my $args = $class->Mousse::Object::BUILDARGS(@_);
4293 my $roles = delete $args->{roles};
4294 my $self = $class->create_anon_role(%{$args});
4295 foreach my $role_spec(@{$roles}) {
4296 my($role, $args) = ref($role_spec) eq 'ARRAY'
4297 ? @{$role_spec}
4298 : ($role_spec, {});
4299 $role->apply($self, %{$args});
4300 }
4301 return $self;
4302 }
4303  
4304 sub get_method_list {
4305 my($self) = @_;
4306 return keys %{ $self->{methods} };
4307 }
4308  
4309 sub add_method {
4310 my($self, $method_name, $code, $role) = @_;
4311  
4312 if( ($self->{methods}{$method_name} || 0) == $code){
4313 # This role already has the same method.
4314 return;
4315 }
4316  
4317 if($method_name eq 'meta'){
4318 $self->SUPER::add_method($method_name => $code);
4319 }
4320 else{
4321 # no need to add a subroutine to the stash
4322 my $roles = $self->{composed_roles_by_method}{$method_name} ||= [];
4323 push @{$roles}, $role;
4324 if(@{$roles} > 1){
4325 $self->{conflicting_methods}{$method_name}++;
4326 }
4327 $self->{methods}{$method_name} = $code;
4328 }
4329 return;
4330 }
4331  
4332 sub get_method_body {
4333 my($self, $method_name) = @_;
4334 return $self->{methods}{$method_name};
4335 }
4336  
4337 sub has_method {
4338 # my($self, $method_name) = @_;
4339 return 0; # to fool apply_methods() in combine()
4340 }
4341  
4342 sub has_attribute {
4343 # my($self, $method_name) = @_;
4344 return 0; # to fool appply_attributes() in combine()
4345 }
4346  
4347 sub has_override_method_modifier {
4348 # my($self, $method_name) = @_;
4349 return 0; # to fool apply_modifiers() in combine()
4350 }
4351  
4352 sub add_attribute {
4353 my $self = shift;
4354 my $attr_name = shift;
4355 my $spec = (@_ == 1 ? $_[0] : {@_});
4356  
4357 my $existing = $self->{attributes}{$attr_name};
4358 if($existing && $existing != $spec){
4359 $self->throw_error("We have encountered an attribute conflict with '$attr_name' "
4360 . "during composition. This is fatal error and cannot be disambiguated.");
4361 }
4362 $self->SUPER::add_attribute($attr_name, $spec);
4363 return;
4364 }
4365  
4366 sub add_override_method_modifier {
4367 my($self, $method_name, $code) = @_;
4368  
4369 my $existing = $self->{override_method_modifiers}{$method_name};
4370 if($existing && $existing != $code){
4371 $self->throw_error( "We have encountered an 'override' method conflict with '$method_name' during "
4372 . "composition (Two 'override' methods of the same name encountered). "
4373 . "This is fatal error.")
4374 }
4375 $self->SUPER::add_override_method_modifier($method_name, $code);
4376 return;
4377 }
4378  
4379 sub apply {
4380 my $self = shift;
4381 my $consumer = shift;
4382  
4383 Mousse::Meta::Role::Application::RoleSummation->new(@_)->apply($self, $consumer);
4384 return;
4385 }
4386  
4387 package Mousse::Meta::Role::Application::RoleSummation;
4388 our @ISA = qw(Mousse::Meta::Role::Application);
4389  
4390 sub apply_methods {
4391 my($self, $role, $consumer, @extra) = @_;
4392  
4393 if(exists $role->{conflicting_methods}){
4394 my $consumer_class_name = $consumer->name;
4395  
4396 my @conflicting = grep{ !$consumer_class_name->can($_) }
4397 keys %{ $role->{conflicting_methods} };
4398  
4399 if(@conflicting) {
4400 my $method_name_conflict = (@conflicting == 1
4401 ? 'a method name conflict'
4402 : 'method name conflicts');
4403  
4404 my %seen;
4405 my $roles = Mousse::Util::quoted_english_list(
4406 grep{ !$seen{$_}++ } # uniq
4407 map { $_->name }
4408 map { @{$_} }
4409 @{ $role->{composed_roles_by_method} }{@conflicting}
4410 );
4411  
4412 $self->throw_error(sprintf
4413 q{Due to %s in roles %s,}
4414 . q{ the method%s %s must be implemented or excluded by '%s'},
4415 $method_name_conflict,
4416 $roles,
4417 (@conflicting > 1 ? 's' : ''),
4418 Mousse::Util::quoted_english_list(@conflicting),
4419 $consumer_class_name);
4420 }
4421 }
4422  
4423 $self->SUPER::apply_methods($role, $consumer, @extra);
4424 return;
4425 }
4426  
4427 package Mousse::Meta::Role::Composite;
4428 # Contents of Mouse::Meta::Role::Method
4429 package Mousse::Meta::Role::Method;
4430 use Mousse::Util; # enables strict and warnings
4431  
4432 use Mousse::Meta::Method;
4433 our @ISA = qw(Mousse::Meta::Method);
4434  
4435 sub _new{
4436 my($class, %args) = @_;
4437 my $self = bless \%args, $class;
4438  
4439 if($class ne __PACKAGE__){
4440 $self->meta->_initialize_object($self, \%args);
4441 }
4442 return $self;
4443 }
4444  
4445 # Contents of Mouse::Object
4446 package Mousse::Object;
4447 use Mousse::Util qw(does dump meta); # enables strict and warnings
4448 # all the stuff are defined in XS or PP
4449  
4450 sub DOES {
4451 my($self, $class_or_role_name) = @_;
4452 return $self->isa($class_or_role_name) || $self->does($class_or_role_name);
4453 }
4454  
4455 # Contents of Mouse::Role
4456 package Mousse::Role;
4457 use Mousse::Exporter; # enables strict and warnings
4458  
4459 our $VERSION = '0.93';
4460  
4461 use Carp ();
4462 use Scalar::Util ();
4463  
4464 use Mousse ();
4465  
4466 Mousse::Exporter->setup_import_methods(
4467 as_is => [qw(
4468 extends with
4469 has
4470 before after around
4471 override super
4472 augment inner
4473  
4474 requires excludes
4475 ),
4476 \&Scalar::Util::blessed,
4477 \&Carp::confess,
4478 ],
4479 );
4480  
4481  
4482 sub extends {
4483 Carp::croak "Roles do not support 'extends'";
4484 }
4485  
4486 sub with {
4487 Mousse::Util::apply_all_roles(scalar(caller), @_);
4488 return;
4489 }
4490  
4491 sub has {
4492 my $meta = Mousse::Meta::Role->initialize(scalar caller);
4493 my $name = shift;
4494  
4495 $meta->throw_error(q{Usage: has 'name' => ( key => value, ... )})
4496 if @_ % 2; # odd number of arguments
4497  
4498 for my $n(ref($name) ? @{$name} : $name){
4499 $meta->add_attribute($n => @_);
4500 }
4501 return;
4502 }
4503  
4504 sub before {
4505 my $meta = Mousse::Meta::Role->initialize(scalar caller);
4506 my $code = pop;
4507 for my $name($meta->_collect_methods(@_)) {
4508 $meta->add_before_method_modifier($name => $code);
4509 }
4510 return;
4511 }
4512  
4513 sub after {
4514 my $meta = Mousse::Meta::Role->initialize(scalar caller);
4515 my $code = pop;
4516 for my $name($meta->_collect_methods(@_)) {
4517 $meta->add_after_method_modifier($name => $code);
4518 }
4519 return;
4520 }
4521  
4522 sub around {
4523 my $meta = Mousse::Meta::Role->initialize(scalar caller);
4524 my $code = pop;
4525 for my $name($meta->_collect_methods(@_)) {
4526 $meta->add_around_method_modifier($name => $code);
4527 }
4528 return;
4529 }
4530  
4531  
4532 sub super {
4533 return if !defined $Mousse::SUPER_BODY;
4534 $Mousse::SUPER_BODY->(@Mousse::SUPER_ARGS);
4535 }
4536  
4537 sub override {
4538 # my($name, $code) = @_;
4539 Mousse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_);
4540 return;
4541 }
4542  
4543 # We keep the same errors messages as Moose::Role emits, here.
4544 sub inner {
4545 Carp::croak "Roles cannot support 'inner'";
4546 }
4547  
4548 sub augment {
4549 Carp::croak "Roles cannot support 'augment'";
4550 }
4551  
4552 sub requires {
4553 my $meta = Mousse::Meta::Role->initialize(scalar caller);
4554 $meta->throw_error("Must specify at least one method") unless @_;
4555 $meta->add_required_methods(@_);
4556 return;
4557 }
4558  
4559 sub excludes {
4560 Mousse::Util::not_supported();
4561 }
4562  
4563 sub init_meta{
4564 shift;
4565 my %args = @_;
4566  
4567 my $class = $args{for_class}
4568 or Carp::confess("Cannot call init_meta without specifying a for_class");
4569  
4570 my $metaclass = $args{metaclass} || 'Mousse::Meta::Role';
4571  
4572 my $meta = $metaclass->initialize($class);
4573  
4574 $meta->add_method(meta => sub{
4575 $metaclass->initialize(ref($_[0]) || $_[0]);
4576 });
4577  
4578 # make a role type for each Mousse role
4579 Mousse::Util::TypeConstraints::role_type($class)
4580 unless Mousse::Util::TypeConstraints::find_type_constraint($class);
4581  
4582 return $meta;
4583 }
4584  
4585 # Contents of Mouse::Util::MetaRole
4586 package Mousse::Util::MetaRole;
4587 use Mousse::Util; # enables strict and warnings
4588 use Scalar::Util ();
4589  
4590 sub apply_metaclass_roles {
4591 my %args = @_;
4592 _fixup_old_style_args(\%args);
4593  
4594 return apply_metaroles(%args);
4595 }
4596  
4597 sub apply_metaroles {
4598 my %args = @_;
4599  
4600 my $for = Scalar::Util::blessed($args{for})
4601 ? $args{for}
4602 : Mousse::Util::get_metaclass_by_name( $args{for} );
4603  
4604 if(!$for){
4605 Carp::confess("You must pass an initialized class, but '$args{for}' has no metaclass");
4606 }
4607  
4608 if ( Mousse::Util::is_a_metarole($for) ) {
4609 return _make_new_metaclass( $for, $args{role_metaroles}, 'role' );
4610 }
4611 else {
4612 return _make_new_metaclass( $for, $args{class_metaroles}, 'class' );
4613 }
4614 }
4615  
4616 sub _make_new_metaclass {
4617 my($for, $roles, $primary) = @_;
4618  
4619 return $for unless keys %{$roles};
4620  
4621 my $new_metaclass = exists($roles->{$primary})
4622 ? _make_new_class( ref $for, $roles->{$primary} ) # new class with traits
4623 : ref $for;
4624  
4625 my %classes;
4626  
4627 for my $key ( grep { $_ ne $primary } keys %{$roles} ) {
4628 my $metaclass;
4629 my $attr = $for->can($metaclass = ($key . '_metaclass'))
4630 || $for->can($metaclass = ($key . '_class'))
4631 || $for->throw_error("Unknown metaclass '$key'");
4632  
4633 $classes{ $metaclass }
4634 = _make_new_class( $for->$attr(), $roles->{$key} );
4635 }
4636  
4637 return $new_metaclass->reinitialize( $for, %classes );
4638 }
4639  
4640  
4641 sub _fixup_old_style_args {
4642 my $args = shift;
4643  
4644 return if $args->{class_metaroles} || $args->{roles_metaroles};
4645  
4646 $args->{for} = delete $args->{for_class}
4647 if exists $args->{for_class};
4648  
4649 my @old_keys = qw(
4650 attribute_metaclass_roles
4651 method_metaclass_roles
4652 wrapped_method_metaclass_roles
4653 instance_metaclass_roles
4654 constructor_class_roles
4655 destructor_class_roles
4656 error_class_roles
4657  
4658 application_to_class_class_roles
4659 application_to_role_class_roles
4660 application_to_instance_class_roles
4661 application_role_summation_class_roles
4662 );
4663  
4664 my $for = Scalar::Util::blessed($args->{for})
4665 ? $args->{for}
4666 : Mousse::Util::get_metaclass_by_name( $args->{for} );
4667  
4668 my $top_key;
4669 if( Mousse::Util::is_a_metaclass($for) ){
4670 $top_key = 'class_metaroles';
4671  
4672 $args->{class_metaroles}{class} = delete $args->{metaclass_roles}
4673 if exists $args->{metaclass_roles};
4674 }
4675 else {
4676 $top_key = 'role_metaroles';
4677  
4678 $args->{role_metaroles}{role} = delete $args->{metaclass_roles}
4679 if exists $args->{metaclass_roles};
4680 }
4681  
4682 for my $old_key (@old_keys) {
4683 my ($new_key) = $old_key =~ /^(.+)_(?:class|metaclass)_roles$/;
4684  
4685 $args->{$top_key}{$new_key} = delete $args->{$old_key}
4686 if exists $args->{$old_key};
4687 }
4688  
4689 return;
4690 }
4691  
4692  
4693 sub apply_base_class_roles {
4694 my %options = @_;
4695  
4696 my $for = $options{for_class};
4697  
4698 my $meta = Mousse::Util::class_of($for);
4699  
4700 my $new_base = _make_new_class(
4701 $for,
4702 $options{roles},
4703 [ $meta->superclasses() ],
4704 );
4705  
4706 $meta->superclasses($new_base)
4707 if $new_base ne $meta->name();
4708 return;
4709 }
4710  
4711 sub _make_new_class {
4712 my($existing_class, $roles, $superclasses) = @_;
4713  
4714 if(!$superclasses){
4715 return $existing_class if !$roles;
4716  
4717 my $meta = Mousse::Meta::Class->initialize($existing_class);
4718  
4719 return $existing_class
4720 if !grep { !ref($_) && !$meta->does_role($_) } @{$roles};
4721 }
4722  
4723 return Mousse::Meta::Class->create_anon_class(
4724 superclasses => $superclasses ? $superclasses : [$existing_class],
4725 roles => $roles,
4726 cache => 1,
4727 )->name();
4728 }
4729  
4730 ;
4731  
4732 package Mousse;
4733  
4734 our $VERSION = '0.13';
4735  
4736 Mousse::Exporter->setup_import_methods(also => 'Mousse::TOP');
4737  
4738 1;
↑ Summary lib/Mousse/Maker.pm »