Commit | Line | Data |
4093c859 |
1 | package Mouse::Util; |
bc69ee88 |
2 | use Mouse::Exporter; # enables strict and warnings |
6d28c5cf |
3 | |
a4b15169 |
4 | # must be here because it will be refered by other modules loaded |
5 | sub get_linear_isa($;$); ## no critic |
ba153b33 |
6 | |
a4b15169 |
7 | # must be here because it will called in Mouse::Exporter |
8 | sub install_subroutines { |
1194aede |
9 | my $into = shift; |
10 | |
11 | while(my($name, $code) = splice @_, 0, 2){ |
12 | no strict 'refs'; |
13 | no warnings 'once', 'redefine'; |
14 | use warnings FATAL => 'uninitialized'; |
15 | *{$into . '::' . $name} = \&{$code}; |
16 | } |
17 | return; |
18 | } |
19 | |
df6dd016 |
20 | BEGIN{ |
8aba926d |
21 | # This is used in Mouse::PurePerl |
22 | Mouse::Exporter->setup_import_methods( |
23 | as_is => [qw( |
24 | find_meta |
25 | does_role |
26 | resolve_metaclass_alias |
27 | apply_all_roles |
28 | english_list |
29 | |
30 | load_class |
31 | is_class_loaded |
32 | |
33 | get_linear_isa |
34 | get_code_info |
35 | |
36 | get_code_package |
37 | get_code_ref |
38 | |
39 | not_supported |
40 | |
41 | does meta dump |
42 | )], |
43 | groups => { |
44 | default => [], # export no functions by default |
45 | |
46 | # The ':meta' group is 'use metaclass' for Mouse |
47 | meta => [qw(does meta dump)], |
48 | }, |
49 | ); |
50 | |
51 | |
df6dd016 |
52 | # Because Mouse::Util is loaded first in all the Mouse sub-modules, |
53 | # XS loader is placed here, not in Mouse.pm. |
54 | |
8f0a0704 |
55 | our $VERSION = '0.59'; |
df6dd016 |
56 | |
db5e4409 |
57 | my $xs = !(exists $INC{'Mouse/PurePerl.pm'} || $ENV{MOUSE_PUREPERL}); |
df6dd016 |
58 | |
db5e4409 |
59 | if($xs){ |
34bdc46a |
60 | # XXX: XSLoader tries to get the object path from caller's file name |
61 | # $hack_mouse_file fools its mechanism |
62 | |
63 | (my $hack_mouse_file = __FILE__) =~ s/.Util//; # .../Mouse/Util.pm -> .../Mouse.pm |
db5e4409 |
64 | $xs = eval sprintf("#line %d %s\n", __LINE__, $hack_mouse_file) . q{ |
d67f600d |
65 | local $^W = 0; # work around 'redefine' warning to &install_subroutines |
df6dd016 |
66 | require XSLoader; |
67 | XSLoader::load('Mouse', $VERSION); |
923a04ba |
68 | Mouse::Util->import({ into => 'Mouse::Meta::Method::Constructor::XS' }, ':meta'); |
69 | Mouse::Util->import({ into => 'Mouse::Meta::Method::Destructor::XS' }, ':meta'); |
3821b191 |
70 | Mouse::Util->import({ into => 'Mouse::Meta::Method::Accessor::XS' }, ':meta'); |
923a04ba |
71 | return 1; |
029463f4 |
72 | } || 0; |
1a6d349c |
73 | #warn $@ if $@; |
df6dd016 |
74 | } |
75 | |
db5e4409 |
76 | if(!$xs){ |
df6dd016 |
77 | require 'Mouse/PurePerl.pm'; # we don't want to create its namespace |
78 | } |
db5e4409 |
79 | |
ecd4a125 |
80 | *MOUSE_XS = sub(){ $xs }; |
df6dd016 |
81 | } |
82 | |
5dd5edef |
83 | use Carp (); |
84 | use Scalar::Util (); |
4093c859 |
85 | |
739525d0 |
86 | # aliases as public APIs |
deb9a0f3 |
87 | # it must be 'require', not 'use', because Mouse::Meta::Module depends on Mouse::Util |
7727a2f0 |
88 | require Mouse::Meta::Module; # for the entities of metaclass cache utilities |
89 | |
01f892fa |
90 | # aliases |
91 | { |
542f20ad |
92 | *class_of = \&Mouse::Meta::Module::_class_of; |
93 | *get_metaclass_by_name = \&Mouse::Meta::Module::_get_metaclass_by_name; |
94 | *get_all_metaclass_instances = \&Mouse::Meta::Module::_get_all_metaclass_instances; |
95 | *get_all_metaclass_names = \&Mouse::Meta::Module::_get_all_metaclass_names; |
f48920c1 |
96 | |
01f892fa |
97 | *Mouse::load_class = \&load_class; |
98 | *Mouse::is_class_loaded = \&is_class_loaded; |
99 | |
f48920c1 |
100 | # is-a predicates |
6a4ab70d |
101 | #generate_isa_predicate_for('Mouse::Meta::TypeConstraint' => 'is_a_type_constraint'); |
102 | #generate_isa_predicate_for('Mouse::Meta::Class' => 'is_a_metaclass'); |
103 | #generate_isa_predicate_for('Mouse::Meta::Role' => 'is_a_metarole'); |
104 | |
105 | # duck type predicates |
106 | generate_can_predicate_for(['_compiled_type_constraint'] => 'is_a_type_constraint'); |
107 | generate_can_predicate_for(['create_anon_class'] => 'is_a_metaclass'); |
108 | generate_can_predicate_for(['create_anon_role'] => 'is_a_metarole'); |
739525d0 |
109 | } |
110 | |
39a8df63 |
111 | our $in_global_destruction = 0; |
112 | END{ $in_global_destruction = 1 } |
f48920c1 |
113 | |
08f7a8db |
114 | # Moose::Util compatible utilities |
115 | |
116 | sub find_meta{ |
739525d0 |
117 | return class_of( $_[0] ); |
08f7a8db |
118 | } |
119 | |
120 | sub does_role{ |
53875581 |
121 | my ($class_or_obj, $role_name) = @_; |
8e64d0fa |
122 | |
739525d0 |
123 | my $meta = class_of($class_or_obj); |
8e64d0fa |
124 | |
53875581 |
125 | (defined $role_name) |
126 | || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()"); |
127 | |
128 | return defined($meta) && $meta->does_role($role_name); |
08f7a8db |
129 | } |
130 | |
42d7df00 |
131 | BEGIN { |
f15868c3 |
132 | my $get_linear_isa; |
bcd39bf4 |
133 | if ($] >= 5.009_005) { |
388b8ebd |
134 | require mro; |
f15868c3 |
135 | $get_linear_isa = \&mro::get_linear_isa; |
272a1930 |
136 | } else { |
74690b26 |
137 | # this code is based on MRO::Compat::__get_linear_isa |
ce5a7699 |
138 | my $_get_linear_isa_dfs; # this recurses so it isn't pretty |
139 | $_get_linear_isa_dfs = sub { |
140 | my($classname) = @_; |
141 | |
142 | my @lin = ($classname); |
143 | my %stored; |
144 | |
145 | no strict 'refs'; |
146 | foreach my $parent (@{"$classname\::ISA"}) { |
74690b26 |
147 | foreach my $p(@{ $_get_linear_isa_dfs->($parent) }) { |
ce5a7699 |
148 | next if exists $stored{$p}; |
149 | push(@lin, $p); |
150 | $stored{$p} = 1; |
272a1930 |
151 | } |
ce5a7699 |
152 | } |
153 | return \@lin; |
154 | }; |
ce5a7699 |
155 | |
74690b26 |
156 | { |
157 | package # hide from PAUSE |
158 | Class::C3; |
159 | our %MRO; # work around 'once' warnings |
160 | } |
ce5a7699 |
161 | |
162 | # MRO::Compat::__get_linear_isa has no prototype, so |
163 | # we define a prototyped version for compatibility with core's |
164 | # See also MRO::Compat::__get_linear_isa. |
165 | $get_linear_isa = sub ($;$){ |
166 | my($classname, $type) = @_; |
74690b26 |
167 | |
ce5a7699 |
168 | if(!defined $type){ |
74690b26 |
169 | $type = exists $Class::C3::MRO{$classname} ? 'c3' : 'dfs'; |
170 | } |
171 | if($type eq 'c3'){ |
172 | require Class::C3; |
173 | return [Class::C3::calculateMRO($classname)]; |
174 | } |
175 | else{ |
176 | return $_get_linear_isa_dfs->($classname); |
ce5a7699 |
177 | } |
ce5a7699 |
178 | }; |
eae80759 |
179 | } |
bcd39bf4 |
180 | |
f15868c3 |
181 | *get_linear_isa = $get_linear_isa; |
eae80759 |
182 | } |
183 | |
3a63a2e7 |
184 | |
08f7a8db |
185 | # taken from Mouse::Util (0.90) |
abfdffe0 |
186 | { |
187 | my %cache; |
188 | |
8e64d0fa |
189 | sub resolve_metaclass_alias { |
190 | my ( $type, $metaclass_name, %options ) = @_; |
191 | |
192 | my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' ); |
193 | |
194 | return $cache{$cache_key}{$metaclass_name} ||= do{ |
abfdffe0 |
195 | |
08f7a8db |
196 | my $possible_full_name = join '::', |
197 | 'Mouse::Meta', $type, 'Custom', ($options{trait} ? 'Trait' : ()), $metaclass_name |
198 | ; |
199 | |
8e64d0fa |
200 | my $loaded_class = load_first_existing_class( |
201 | $possible_full_name, |
202 | $metaclass_name |
203 | ); |
204 | |
205 | $loaded_class->can('register_implementation') |
206 | ? $loaded_class->register_implementation |
08f7a8db |
207 | : $loaded_class; |
8e64d0fa |
208 | }; |
abfdffe0 |
209 | } |
210 | } |
211 | |
739525d0 |
212 | # Utilities from Class::MOP |
213 | |
df6dd016 |
214 | sub get_code_info; |
215 | sub get_code_package; |
739525d0 |
216 | |
0ffc4183 |
217 | sub is_valid_class_name; |
abfdffe0 |
218 | |
219 | # taken from Class/MOP.pm |
220 | sub load_first_existing_class { |
221 | my @classes = @_ |
222 | or return; |
223 | |
23264b5b |
224 | my %exceptions; |
225 | for my $class (@classes) { |
abfdffe0 |
226 | my $e = _try_load_one_class($class); |
227 | |
228 | if ($e) { |
229 | $exceptions{$class} = $e; |
230 | } |
231 | else { |
53875581 |
232 | return $class; |
abfdffe0 |
233 | } |
234 | } |
abfdffe0 |
235 | |
53875581 |
236 | # not found |
5dd5edef |
237 | Carp::confess join( |
abfdffe0 |
238 | "\n", |
239 | map { |
240 | sprintf( "Could not load class (%s) because : %s", |
241 | $_, $exceptions{$_} ) |
242 | } @classes |
243 | ); |
244 | } |
245 | |
246 | # taken from Class/MOP.pm |
247 | sub _try_load_one_class { |
248 | my $class = shift; |
249 | |
6cfa1e5e |
250 | unless ( is_valid_class_name($class) ) { |
251 | my $display = defined($class) ? $class : 'undef'; |
5dd5edef |
252 | Carp::confess "Invalid class name ($display)"; |
6cfa1e5e |
253 | } |
254 | |
be0ba859 |
255 | return '' if is_class_loaded($class); |
abfdffe0 |
256 | |
637d4f17 |
257 | $class =~ s{::}{/}g; |
258 | $class .= '.pm'; |
abfdffe0 |
259 | |
260 | return do { |
261 | local $@; |
637d4f17 |
262 | eval { require $class }; |
abfdffe0 |
263 | $@; |
264 | }; |
265 | } |
266 | |
6cfa1e5e |
267 | |
268 | sub load_class { |
269 | my $class = shift; |
270 | my $e = _try_load_one_class($class); |
5dd5edef |
271 | Carp::confess "Could not load class ($class) because : $e" if $e; |
6cfa1e5e |
272 | |
637d4f17 |
273 | return $class; |
6cfa1e5e |
274 | } |
275 | |
df6dd016 |
276 | sub is_class_loaded; |
6cfa1e5e |
277 | |
2e92bb89 |
278 | sub apply_all_roles { |
45f22b92 |
279 | my $consumer = Scalar::Util::blessed($_[0]) |
5dd5edef |
280 | ? shift # instance |
281 | : Mouse::Meta::Class->initialize(shift); # class or role name |
2e92bb89 |
282 | |
21498b08 |
283 | my @roles; |
f6715552 |
284 | |
285 | # Basis of Data::OptList |
21498b08 |
286 | my $max = scalar(@_); |
287 | for (my $i = 0; $i < $max ; $i++) { |
288 | if ($i + 1 < $max && ref($_[$i + 1])) { |
b1980b86 |
289 | push @roles, [ $_[$i] => $_[++$i] ]; |
21498b08 |
290 | } else { |
b1980b86 |
291 | push @roles, [ $_[$i] => undef ]; |
21498b08 |
292 | } |
ff687069 |
293 | my $role_name = $roles[-1][0]; |
294 | load_class($role_name); |
0126c27c |
295 | |
f48920c1 |
296 | is_a_metarole( get_metaclass_by_name($role_name) ) |
45f22b92 |
297 | || $consumer->meta->throw_error("You can only consume roles, $role_name is not a Mouse role"); |
21498b08 |
298 | } |
299 | |
21498b08 |
300 | if ( scalar @roles == 1 ) { |
b1980b86 |
301 | my ( $role_name, $params ) = @{ $roles[0] }; |
45f22b92 |
302 | get_metaclass_by_name($role_name)->apply( $consumer, defined $params ? $params : () ); |
21498b08 |
303 | } |
304 | else { |
45f22b92 |
305 | Mouse::Meta::Role->combine(@roles)->apply($consumer); |
21498b08 |
306 | } |
23264b5b |
307 | return; |
2e92bb89 |
308 | } |
309 | |
2e33bb59 |
310 | # taken from Moose::Util 0.90 |
311 | sub english_list { |
8e64d0fa |
312 | return $_[0] if @_ == 1; |
313 | |
314 | my @items = sort @_; |
315 | |
316 | return "$items[0] and $items[1]" if @items == 2; |
317 | |
318 | my $tail = pop @items; |
319 | |
320 | return join q{, }, @items, "and $tail"; |
2e33bb59 |
321 | } |
322 | |
5af36247 |
323 | sub quoted_english_list { |
53f661ad |
324 | return english_list(map { qq{'$_'} } @_); |
5af36247 |
325 | } |
326 | |
53875581 |
327 | # common utilities |
328 | |
fce211ae |
329 | sub not_supported{ |
330 | my($feature) = @_; |
331 | |
332 | $feature ||= ( caller(1) )[3]; # subroutine name |
333 | |
1b9e472d |
334 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; |
335 | Carp::confess("Mouse does not currently support $feature"); |
fce211ae |
336 | } |
337 | |
fc8628e3 |
338 | # general meta() method |
339 | sub meta :method{ |
152e5759 |
340 | return Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]); |
53875581 |
341 | } |
342 | |
fc8628e3 |
343 | # general dump() method |
344 | sub dump :method { |
53875581 |
345 | my($self, $maxdepth) = @_; |
346 | |
347 | require 'Data/Dumper.pm'; # we don't want to create its namespace |
348 | my $dd = Data::Dumper->new([$self]); |
0cf6f1be |
349 | $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 3); |
53875581 |
350 | $dd->Indent(1); |
351 | return $dd->Dump(); |
352 | } |
353 | |
fc8628e3 |
354 | # general does() method |
b64e2007 |
355 | sub does :method { |
356 | goto &does_role; |
357 | } |
53875581 |
358 | |
4093c859 |
359 | 1; |
f38ce2d0 |
360 | __END__ |
361 | |
362 | =head1 NAME |
363 | |
bedd575c |
364 | Mouse::Util - Features, with or without their dependencies |
f38ce2d0 |
365 | |
a25ca8d6 |
366 | =head1 VERSION |
367 | |
8f0a0704 |
368 | This document describes Mouse version 0.59 |
a25ca8d6 |
369 | |
f38ce2d0 |
370 | =head1 IMPLEMENTATIONS FOR |
371 | |
ea249879 |
372 | =head2 Moose::Util |
373 | |
374 | =head3 C<find_meta> |
375 | |
376 | =head3 C<does_role> |
377 | |
378 | =head3 C<resolve_metaclass_alias> |
379 | |
380 | =head3 C<apply_all_roles> |
381 | |
382 | =head3 C<english_list> |
383 | |
384 | =head2 Class::MOP |
385 | |
5bacd9bf |
386 | =head3 C<< is_class_loaded(ClassName) -> Bool >> |
ea249879 |
387 | |
1820fffe |
388 | Returns whether C<ClassName> is actually loaded or not. It uses a heuristic which |
389 | involves checking for the existence of C<$VERSION>, C<@ISA>, and any |
390 | locally-defined method. |
391 | |
392 | =head3 C<< load_class(ClassName) >> |
393 | |
739525d0 |
394 | This will load a given C<ClassName> (or die if it is not loadable). |
1820fffe |
395 | This function can be used in place of tricks like |
396 | C<eval "use $module"> or using C<require>. |
ea249879 |
397 | |
5164490d |
398 | =head3 C<< Mouse::Util::class_of(ClassName or Object) >> |
739525d0 |
399 | |
5164490d |
400 | =head3 C<< Mouse::Util::get_metaclass_by_name(ClassName) >> |
739525d0 |
401 | |
5164490d |
402 | =head3 C<< Mouse::Util::get_all_metaclass_instances() >> |
739525d0 |
403 | |
5164490d |
404 | =head3 C<< Mouse::Util::get_all_metaclass_names() >> |
739525d0 |
405 | |
ea249879 |
406 | =head2 MRO::Compat |
407 | |
408 | =head3 C<get_linear_isa> |
409 | |
410 | =head2 Sub::Identify |
411 | |
412 | =head3 C<get_code_info> |
413 | |
5482cd4c |
414 | =head1 Mouse specific utilities |
ea249879 |
415 | |
bedd575c |
416 | =head3 C<not_supported> |
f38ce2d0 |
417 | |
5482cd4c |
418 | =head3 C<get_code_package> |
419 | |
420 | =head3 C<get_code_ref> |
421 | |
1820fffe |
422 | =head1 SEE ALSO |
423 | |
424 | L<Moose::Util> |
425 | |
5164490d |
426 | L<Class::MOP> |
1820fffe |
427 | |
428 | L<Sub::Identify> |
429 | |
430 | L<MRO::Compat> |
431 | |
f38ce2d0 |
432 | =cut |
433 | |