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 | |
4c0fe06f |
41 | our $VERSION = '0.41'; |
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; |
df6dd016 |
57 | }; |
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 | |
77 | BEGIN { |
542f20ad |
78 | *class_of = \&Mouse::Meta::Module::_class_of; |
79 | *get_metaclass_by_name = \&Mouse::Meta::Module::_get_metaclass_by_name; |
80 | *get_all_metaclass_instances = \&Mouse::Meta::Module::_get_all_metaclass_instances; |
81 | *get_all_metaclass_names = \&Mouse::Meta::Module::_get_all_metaclass_names; |
f48920c1 |
82 | |
83 | # is-a predicates |
e3540312 |
84 | generate_isa_predicate_for('Mouse::Meta::TypeConstraint' => 'is_a_type_constraint'); |
85 | generate_isa_predicate_for('Mouse::Meta::Class' => 'is_a_metaclass'); |
86 | generate_isa_predicate_for('Mouse::Meta::Role' => 'is_a_metarole'); |
739525d0 |
87 | } |
88 | |
39a8df63 |
89 | our $in_global_destruction = 0; |
90 | END{ $in_global_destruction = 1 } |
f48920c1 |
91 | |
08f7a8db |
92 | # Moose::Util compatible utilities |
93 | |
94 | sub find_meta{ |
739525d0 |
95 | return class_of( $_[0] ); |
08f7a8db |
96 | } |
97 | |
98 | sub does_role{ |
53875581 |
99 | my ($class_or_obj, $role_name) = @_; |
8e64d0fa |
100 | |
739525d0 |
101 | my $meta = class_of($class_or_obj); |
8e64d0fa |
102 | |
53875581 |
103 | (defined $role_name) |
104 | || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()"); |
105 | |
106 | return defined($meta) && $meta->does_role($role_name); |
08f7a8db |
107 | } |
108 | |
42d7df00 |
109 | BEGIN { |
f15868c3 |
110 | my $get_linear_isa; |
bcd39bf4 |
111 | if ($] >= 5.009_005) { |
388b8ebd |
112 | require mro; |
f15868c3 |
113 | $get_linear_isa = \&mro::get_linear_isa; |
272a1930 |
114 | } else { |
272a1930 |
115 | # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV |
ce5a7699 |
116 | my $_get_linear_isa_dfs; # this recurses so it isn't pretty |
117 | $_get_linear_isa_dfs = sub { |
118 | my($classname) = @_; |
119 | |
120 | my @lin = ($classname); |
121 | my %stored; |
122 | |
123 | no strict 'refs'; |
124 | foreach my $parent (@{"$classname\::ISA"}) { |
125 | my $plin = $_get_linear_isa_dfs->($parent); |
126 | foreach my $p(@$plin) { |
127 | next if exists $stored{$p}; |
128 | push(@lin, $p); |
129 | $stored{$p} = 1; |
272a1930 |
130 | } |
ce5a7699 |
131 | } |
132 | return \@lin; |
133 | }; |
272a1930 |
134 | # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^ |
ce5a7699 |
135 | |
136 | eval{ require Class::C3 }; |
137 | |
138 | # MRO::Compat::__get_linear_isa has no prototype, so |
139 | # we define a prototyped version for compatibility with core's |
140 | # See also MRO::Compat::__get_linear_isa. |
141 | $get_linear_isa = sub ($;$){ |
142 | my($classname, $type) = @_; |
143 | if(!defined $type){ |
144 | package Class::C3; |
145 | our %MRO; |
146 | $type = exists $MRO{$classname} ? 'c3' : 'dfs'; |
147 | } |
148 | return $type eq 'c3' |
149 | ? [Class::C3::calculateMRO($classname)] |
150 | : $_get_linear_isa_dfs->($classname);\r |
151 | }; |
eae80759 |
152 | } |
bcd39bf4 |
153 | |
f15868c3 |
154 | *get_linear_isa = $get_linear_isa; |
eae80759 |
155 | } |
156 | |
3a63a2e7 |
157 | |
08f7a8db |
158 | # taken from Mouse::Util (0.90) |
abfdffe0 |
159 | { |
160 | my %cache; |
161 | |
8e64d0fa |
162 | sub resolve_metaclass_alias { |
163 | my ( $type, $metaclass_name, %options ) = @_; |
164 | |
165 | my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' ); |
166 | |
167 | return $cache{$cache_key}{$metaclass_name} ||= do{ |
abfdffe0 |
168 | |
08f7a8db |
169 | my $possible_full_name = join '::', |
170 | 'Mouse::Meta', $type, 'Custom', ($options{trait} ? 'Trait' : ()), $metaclass_name |
171 | ; |
172 | |
8e64d0fa |
173 | my $loaded_class = load_first_existing_class( |
174 | $possible_full_name, |
175 | $metaclass_name |
176 | ); |
177 | |
178 | $loaded_class->can('register_implementation') |
179 | ? $loaded_class->register_implementation |
08f7a8db |
180 | : $loaded_class; |
8e64d0fa |
181 | }; |
abfdffe0 |
182 | } |
183 | } |
184 | |
739525d0 |
185 | # Utilities from Class::MOP |
186 | |
df6dd016 |
187 | sub get_code_info; |
188 | sub get_code_package; |
739525d0 |
189 | |
abfdffe0 |
190 | # taken from Class/MOP.pm |
a81cc7b8 |
191 | sub is_valid_class_name { |
abfdffe0 |
192 | my $class = shift; |
193 | |
194 | return 0 if ref($class); |
195 | return 0 unless defined($class); |
abfdffe0 |
196 | |
ba153b33 |
197 | return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms; |
abfdffe0 |
198 | |
199 | return 0; |
200 | } |
201 | |
202 | # taken from Class/MOP.pm |
203 | sub load_first_existing_class { |
204 | my @classes = @_ |
205 | or return; |
206 | |
23264b5b |
207 | my %exceptions; |
208 | for my $class (@classes) { |
abfdffe0 |
209 | my $e = _try_load_one_class($class); |
210 | |
211 | if ($e) { |
212 | $exceptions{$class} = $e; |
213 | } |
214 | else { |
53875581 |
215 | return $class; |
abfdffe0 |
216 | } |
217 | } |
abfdffe0 |
218 | |
53875581 |
219 | # not found |
5dd5edef |
220 | Carp::confess join( |
abfdffe0 |
221 | "\n", |
222 | map { |
223 | sprintf( "Could not load class (%s) because : %s", |
224 | $_, $exceptions{$_} ) |
225 | } @classes |
226 | ); |
227 | } |
228 | |
229 | # taken from Class/MOP.pm |
3aac966d |
230 | my %is_class_loaded_cache; |
abfdffe0 |
231 | sub _try_load_one_class { |
232 | my $class = shift; |
233 | |
6cfa1e5e |
234 | unless ( is_valid_class_name($class) ) { |
235 | my $display = defined($class) ? $class : 'undef'; |
5dd5edef |
236 | Carp::confess "Invalid class name ($display)"; |
6cfa1e5e |
237 | } |
238 | |
3aac966d |
239 | return undef if $is_class_loaded_cache{$class} ||= is_class_loaded($class); |
abfdffe0 |
240 | |
241 | my $file = $class . '.pm'; |
242 | $file =~ s{::}{/}g; |
243 | |
244 | return do { |
245 | local $@; |
246 | eval { require($file) }; |
247 | $@; |
248 | }; |
249 | } |
250 | |
6cfa1e5e |
251 | |
252 | sub load_class { |
253 | my $class = shift; |
254 | my $e = _try_load_one_class($class); |
5dd5edef |
255 | Carp::confess "Could not load class ($class) because : $e" if $e; |
6cfa1e5e |
256 | |
257 | return 1; |
258 | } |
259 | |
df6dd016 |
260 | sub is_class_loaded; |
6cfa1e5e |
261 | |
2e92bb89 |
262 | sub apply_all_roles { |
5dd5edef |
263 | my $applicant = Scalar::Util::blessed($_[0]) |
264 | ? shift # instance |
265 | : Mouse::Meta::Class->initialize(shift); # class or role name |
2e92bb89 |
266 | |
21498b08 |
267 | my @roles; |
f6715552 |
268 | |
269 | # Basis of Data::OptList |
21498b08 |
270 | my $max = scalar(@_); |
271 | for (my $i = 0; $i < $max ; $i++) { |
272 | if ($i + 1 < $max && ref($_[$i + 1])) { |
b1980b86 |
273 | push @roles, [ $_[$i] => $_[++$i] ]; |
21498b08 |
274 | } else { |
b1980b86 |
275 | push @roles, [ $_[$i] => undef ]; |
21498b08 |
276 | } |
ff687069 |
277 | my $role_name = $roles[-1][0]; |
278 | load_class($role_name); |
0126c27c |
279 | |
f48920c1 |
280 | is_a_metarole( get_metaclass_by_name($role_name) ) |
9ad58623 |
281 | || $applicant->meta->throw_error("You can only consume roles, $role_name is not a Mouse role"); |
21498b08 |
282 | } |
283 | |
21498b08 |
284 | if ( scalar @roles == 1 ) { |
b1980b86 |
285 | my ( $role_name, $params ) = @{ $roles[0] }; |
286 | get_metaclass_by_name($role_name)->apply( $applicant, defined $params ? $params : () ); |
21498b08 |
287 | } |
288 | else { |
71e7b544 |
289 | Mouse::Meta::Role->combine(@roles)->apply($applicant); |
21498b08 |
290 | } |
23264b5b |
291 | return; |
2e92bb89 |
292 | } |
293 | |
2e33bb59 |
294 | # taken from Moose::Util 0.90 |
295 | sub english_list { |
8e64d0fa |
296 | return $_[0] if @_ == 1; |
297 | |
298 | my @items = sort @_; |
299 | |
300 | return "$items[0] and $items[1]" if @items == 2; |
301 | |
302 | my $tail = pop @items; |
303 | |
304 | return join q{, }, @items, "and $tail"; |
2e33bb59 |
305 | } |
306 | |
53875581 |
307 | # common utilities |
308 | |
fce211ae |
309 | sub not_supported{ |
310 | my($feature) = @_; |
311 | |
312 | $feature ||= ( caller(1) )[3]; # subroutine name |
313 | |
1b9e472d |
314 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; |
315 | Carp::confess("Mouse does not currently support $feature"); |
fce211ae |
316 | } |
317 | |
fc8628e3 |
318 | # general meta() method |
319 | sub meta :method{ |
152e5759 |
320 | return Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]); |
53875581 |
321 | } |
322 | |
fc8628e3 |
323 | # general dump() method |
324 | sub dump :method { |
53875581 |
325 | my($self, $maxdepth) = @_; |
326 | |
327 | require 'Data/Dumper.pm'; # we don't want to create its namespace |
328 | my $dd = Data::Dumper->new([$self]); |
329 | $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 2); |
330 | $dd->Indent(1); |
331 | return $dd->Dump(); |
332 | } |
333 | |
fc8628e3 |
334 | # general does() method |
53875581 |
335 | sub does :method; |
336 | *does = \&does_role; # alias |
337 | |
4093c859 |
338 | 1; |
f38ce2d0 |
339 | __END__ |
340 | |
341 | =head1 NAME |
342 | |
bedd575c |
343 | Mouse::Util - Features, with or without their dependencies |
f38ce2d0 |
344 | |
a25ca8d6 |
345 | =head1 VERSION |
346 | |
4c0fe06f |
347 | This document describes Mouse version 0.41 |
a25ca8d6 |
348 | |
f38ce2d0 |
349 | =head1 IMPLEMENTATIONS FOR |
350 | |
ea249879 |
351 | =head2 Moose::Util |
352 | |
353 | =head3 C<find_meta> |
354 | |
355 | =head3 C<does_role> |
356 | |
357 | =head3 C<resolve_metaclass_alias> |
358 | |
359 | =head3 C<apply_all_roles> |
360 | |
361 | =head3 C<english_list> |
362 | |
363 | =head2 Class::MOP |
364 | |
5bacd9bf |
365 | =head3 C<< is_class_loaded(ClassName) -> Bool >> |
ea249879 |
366 | |
1820fffe |
367 | Returns whether C<ClassName> is actually loaded or not. It uses a heuristic which |
368 | involves checking for the existence of C<$VERSION>, C<@ISA>, and any |
369 | locally-defined method. |
370 | |
371 | =head3 C<< load_class(ClassName) >> |
372 | |
739525d0 |
373 | This will load a given C<ClassName> (or die if it is not loadable). |
1820fffe |
374 | This function can be used in place of tricks like |
375 | C<eval "use $module"> or using C<require>. |
ea249879 |
376 | |
5164490d |
377 | =head3 C<< Mouse::Util::class_of(ClassName or Object) >> |
739525d0 |
378 | |
5164490d |
379 | =head3 C<< Mouse::Util::get_metaclass_by_name(ClassName) >> |
739525d0 |
380 | |
5164490d |
381 | =head3 C<< Mouse::Util::get_all_metaclass_instances() >> |
739525d0 |
382 | |
5164490d |
383 | =head3 C<< Mouse::Util::get_all_metaclass_names() >> |
739525d0 |
384 | |
ea249879 |
385 | =head2 MRO::Compat |
386 | |
387 | =head3 C<get_linear_isa> |
388 | |
389 | =head2 Sub::Identify |
390 | |
391 | =head3 C<get_code_info> |
392 | |
5482cd4c |
393 | =head1 Mouse specific utilities |
ea249879 |
394 | |
bedd575c |
395 | =head3 C<not_supported> |
f38ce2d0 |
396 | |
5482cd4c |
397 | =head3 C<get_code_package> |
398 | |
399 | =head3 C<get_code_ref> |
400 | |
1820fffe |
401 | =head1 SEE ALSO |
402 | |
403 | L<Moose::Util> |
404 | |
5164490d |
405 | L<Class::MOP> |
1820fffe |
406 | |
407 | L<Sub::Identify> |
408 | |
409 | L<MRO::Compat> |
410 | |
f38ce2d0 |
411 | =cut |
412 | |