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