Commit | Line | Data |
4093c859 |
1 | package Mouse::Util; |
bc69ee88 |
2 | use Mouse::Exporter; # enables strict and warnings |
6d28c5cf |
3 | |
3a63a2e7 |
4 | use Carp qw(confess); |
71e7b544 |
5 | use Scalar::Util qw(blessed); |
01afd8ff |
6 | use B (); |
4093c859 |
7 | |
04493075 |
8 | use constant _MOUSE_VERBOSE => !!$ENV{MOUSE_VERBOSE}; |
9 | |
bc69ee88 |
10 | Mouse::Exporter->setup_import_methods( |
11 | as_is => [qw( |
12 | find_meta |
13 | does_role |
14 | resolve_metaclass_alias |
15 | apply_all_roles |
16 | english_list |
08f7a8db |
17 | |
bc69ee88 |
18 | load_class |
19 | is_class_loaded |
08f7a8db |
20 | |
bc69ee88 |
21 | get_linear_isa |
22 | get_code_info |
ea249879 |
23 | |
bc69ee88 |
24 | get_code_package |
01afd8ff |
25 | |
bc69ee88 |
26 | not_supported |
53875581 |
27 | |
bc69ee88 |
28 | does meta dump |
29 | _MOUSE_VERBOSE |
30 | )], |
31 | groups => { |
32 | default => [], # export no functions by default |
0126c27c |
33 | |
34 | # The ':meta' group is 'use metaclass' for Mouse |
bc69ee88 |
35 | meta => [qw(does meta dump _MOUSE_VERBOSE)], |
36 | }, |
eae80759 |
37 | ); |
38 | |
739525d0 |
39 | # aliases as public APIs |
deb9a0f3 |
40 | # it must be 'require', not 'use', because Mouse::Meta::Module depends on Mouse::Util |
7727a2f0 |
41 | require Mouse::Meta::Module; # for the entities of metaclass cache utilities |
42 | |
43 | BEGIN { |
739525d0 |
44 | *class_of = \&Mouse::Meta::Module::class_of; |
45 | *get_metaclass_by_name = \&Mouse::Meta::Module::get_metaclass_by_name; |
46 | *get_all_metaclass_instances = \&Mouse::Meta::Module::get_all_metaclass_instances; |
47 | *get_all_metaclass_names = \&Mouse::Meta::Module::get_all_metaclass_names; |
48 | } |
49 | |
08f7a8db |
50 | # Moose::Util compatible utilities |
51 | |
52 | sub find_meta{ |
739525d0 |
53 | return class_of( $_[0] ); |
08f7a8db |
54 | } |
55 | |
56 | sub does_role{ |
53875581 |
57 | my ($class_or_obj, $role_name) = @_; |
8e64d0fa |
58 | |
739525d0 |
59 | my $meta = class_of($class_or_obj); |
8e64d0fa |
60 | |
53875581 |
61 | (defined $role_name) |
62 | || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()"); |
63 | |
64 | return defined($meta) && $meta->does_role($role_name); |
08f7a8db |
65 | } |
66 | |
42d7df00 |
67 | BEGIN { |
272a1930 |
68 | my $impl; |
bcd39bf4 |
69 | if ($] >= 5.009_005) { |
388b8ebd |
70 | require mro; |
272a1930 |
71 | $impl = \&mro::get_linear_isa; |
72 | } else { |
e0396a57 |
73 | my $e = do { |
74 | local $@; |
75 | eval { require MRO::Compat }; |
76 | $@; |
f172d4e7 |
77 | }; |
e0396a57 |
78 | if (!$e) { |
272a1930 |
79 | $impl = \&mro::get_linear_isa; |
80 | } else { |
81 | # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV |
e0396a57 |
82 | my $_get_linear_isa_dfs; # this recurses so it isn't pretty |
83 | $_get_linear_isa_dfs = sub { |
272a1930 |
84 | no strict 'refs'; |
85 | |
86 | my $classname = shift; |
87 | |
88 | my @lin = ($classname); |
89 | my %stored; |
90 | foreach my $parent (@{"$classname\::ISA"}) { |
e0396a57 |
91 | my $plin = $_get_linear_isa_dfs->($parent); |
92 | foreach my $p(@$plin) { |
93 | next if exists $stored{$p}; |
94 | push(@lin, $p); |
95 | $stored{$p} = 1; |
272a1930 |
96 | } |
97 | } |
98 | return \@lin; |
99 | }; |
100 | # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^ |
e0396a57 |
101 | $impl = $_get_linear_isa_dfs; |
eae80759 |
102 | } |
103 | } |
bcd39bf4 |
104 | |
a81cc7b8 |
105 | |
106 | no warnings 'once'; |
107 | *get_linear_isa = $impl; |
eae80759 |
108 | } |
109 | |
3a63a2e7 |
110 | { # taken from Sub::Identify |
8e64d0fa |
111 | sub get_code_info($) { |
112 | my ($coderef) = @_; |
113 | ref($coderef) or return; |
23264b5b |
114 | |
8e64d0fa |
115 | my $cv = B::svref_2object($coderef); |
3a63a2e7 |
116 | $cv->isa('B::CV') or return; |
117 | |
8e64d0fa |
118 | my $gv = $cv->GV; |
119 | $gv->isa('B::GV') or return; |
120 | |
121 | return ($gv->STASH->NAME, $gv->NAME); |
122 | } |
01afd8ff |
123 | |
124 | sub get_code_package{ |
125 | my($coderef) = @_; |
126 | |
127 | my $cv = B::svref_2object($coderef); |
128 | $cv->isa('B::CV') or return ''; |
129 | |
130 | my $gv = $cv->GV; |
131 | $gv->isa('B::GV') or return ''; |
132 | |
133 | return $gv->STASH->NAME; |
134 | } |
3a63a2e7 |
135 | } |
136 | |
08f7a8db |
137 | # taken from Mouse::Util (0.90) |
abfdffe0 |
138 | { |
139 | my %cache; |
140 | |
8e64d0fa |
141 | sub resolve_metaclass_alias { |
142 | my ( $type, $metaclass_name, %options ) = @_; |
143 | |
144 | my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' ); |
145 | |
146 | return $cache{$cache_key}{$metaclass_name} ||= do{ |
abfdffe0 |
147 | |
08f7a8db |
148 | my $possible_full_name = join '::', |
149 | 'Mouse::Meta', $type, 'Custom', ($options{trait} ? 'Trait' : ()), $metaclass_name |
150 | ; |
151 | |
8e64d0fa |
152 | my $loaded_class = load_first_existing_class( |
153 | $possible_full_name, |
154 | $metaclass_name |
155 | ); |
156 | |
157 | $loaded_class->can('register_implementation') |
158 | ? $loaded_class->register_implementation |
08f7a8db |
159 | : $loaded_class; |
8e64d0fa |
160 | }; |
abfdffe0 |
161 | } |
162 | } |
163 | |
739525d0 |
164 | # Utilities from Class::MOP |
165 | |
166 | |
abfdffe0 |
167 | # taken from Class/MOP.pm |
a81cc7b8 |
168 | sub is_valid_class_name { |
abfdffe0 |
169 | my $class = shift; |
170 | |
171 | return 0 if ref($class); |
172 | return 0 unless defined($class); |
abfdffe0 |
173 | |
174 | return 1 if $class =~ /^\w+(?:::\w+)*$/; |
175 | |
176 | return 0; |
177 | } |
178 | |
179 | # taken from Class/MOP.pm |
180 | sub load_first_existing_class { |
181 | my @classes = @_ |
182 | or return; |
183 | |
23264b5b |
184 | my %exceptions; |
185 | for my $class (@classes) { |
abfdffe0 |
186 | my $e = _try_load_one_class($class); |
187 | |
188 | if ($e) { |
189 | $exceptions{$class} = $e; |
190 | } |
191 | else { |
53875581 |
192 | return $class; |
abfdffe0 |
193 | } |
194 | } |
abfdffe0 |
195 | |
53875581 |
196 | # not found |
abfdffe0 |
197 | confess join( |
198 | "\n", |
199 | map { |
200 | sprintf( "Could not load class (%s) because : %s", |
201 | $_, $exceptions{$_} ) |
202 | } @classes |
203 | ); |
204 | } |
205 | |
206 | # taken from Class/MOP.pm |
3aac966d |
207 | my %is_class_loaded_cache; |
abfdffe0 |
208 | sub _try_load_one_class { |
209 | my $class = shift; |
210 | |
6cfa1e5e |
211 | unless ( is_valid_class_name($class) ) { |
212 | my $display = defined($class) ? $class : 'undef'; |
213 | confess "Invalid class name ($display)"; |
214 | } |
215 | |
3aac966d |
216 | return undef if $is_class_loaded_cache{$class} ||= is_class_loaded($class); |
abfdffe0 |
217 | |
218 | my $file = $class . '.pm'; |
219 | $file =~ s{::}{/}g; |
220 | |
221 | return do { |
222 | local $@; |
223 | eval { require($file) }; |
224 | $@; |
225 | }; |
226 | } |
227 | |
6cfa1e5e |
228 | |
229 | sub load_class { |
230 | my $class = shift; |
231 | my $e = _try_load_one_class($class); |
232 | confess "Could not load class ($class) because : $e" if $e; |
233 | |
234 | return 1; |
235 | } |
236 | |
3aac966d |
237 | |
6cfa1e5e |
238 | sub is_class_loaded { |
239 | my $class = shift; |
240 | |
241 | return 0 if ref($class) || !defined($class) || !length($class); |
242 | |
6cfa1e5e |
243 | # walk the symbol table tree to avoid autovififying |
244 | # \*{${main::}{"Foo::"}} == \*main::Foo:: |
245 | |
ff687069 |
246 | my $pack = \%::; |
6cfa1e5e |
247 | foreach my $part (split('::', $class)) { |
ff687069 |
248 | my $entry = \$pack->{$part . '::'}; |
249 | return 0 if ref($entry) ne 'GLOB'; |
250 | $pack = *{$entry}{HASH} or return 0; |
6cfa1e5e |
251 | } |
252 | |
253 | # check for $VERSION or @ISA |
3aac966d |
254 | return 1 if exists $pack->{VERSION} |
ff687069 |
255 | && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} }; |
3aac966d |
256 | return 1 if exists $pack->{ISA} |
ff687069 |
257 | && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0; |
6cfa1e5e |
258 | |
259 | # check for any method |
ff687069 |
260 | foreach my $name( keys %{$pack} ) { |
261 | my $entry = \$pack->{$name}; |
3aac966d |
262 | return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE}; |
6cfa1e5e |
263 | } |
264 | |
265 | # fail |
266 | return 0; |
267 | } |
268 | |
269 | |
2e92bb89 |
270 | sub apply_all_roles { |
71e7b544 |
271 | my $applicant = blessed($_[0]) ? shift : Mouse::Meta::Class->initialize(shift); |
2e92bb89 |
272 | |
21498b08 |
273 | my @roles; |
f6715552 |
274 | |
275 | # Basis of Data::OptList |
21498b08 |
276 | my $max = scalar(@_); |
277 | for (my $i = 0; $i < $max ; $i++) { |
278 | if ($i + 1 < $max && ref($_[$i + 1])) { |
b1980b86 |
279 | push @roles, [ $_[$i] => $_[++$i] ]; |
21498b08 |
280 | } else { |
b1980b86 |
281 | push @roles, [ $_[$i] => undef ]; |
21498b08 |
282 | } |
ff687069 |
283 | my $role_name = $roles[-1][0]; |
284 | load_class($role_name); |
0126c27c |
285 | |
b1980b86 |
286 | my $metarole = get_metaclass_by_name($role_name); |
287 | ( $metarole && $metarole->isa('Mouse::Meta::Role') ) |
71e7b544 |
288 | || $applicant->meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role"); |
21498b08 |
289 | } |
290 | |
21498b08 |
291 | if ( scalar @roles == 1 ) { |
b1980b86 |
292 | my ( $role_name, $params ) = @{ $roles[0] }; |
293 | get_metaclass_by_name($role_name)->apply( $applicant, defined $params ? $params : () ); |
21498b08 |
294 | } |
295 | else { |
71e7b544 |
296 | Mouse::Meta::Role->combine(@roles)->apply($applicant); |
21498b08 |
297 | } |
23264b5b |
298 | return; |
2e92bb89 |
299 | } |
300 | |
2e33bb59 |
301 | # taken from Moose::Util 0.90 |
302 | sub english_list { |
8e64d0fa |
303 | return $_[0] if @_ == 1; |
304 | |
305 | my @items = sort @_; |
306 | |
307 | return "$items[0] and $items[1]" if @items == 2; |
308 | |
309 | my $tail = pop @items; |
310 | |
311 | return join q{, }, @items, "and $tail"; |
2e33bb59 |
312 | } |
313 | |
53875581 |
314 | |
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]); |
337 | $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 2); |
338 | $dd->Indent(1); |
339 | return $dd->Dump(); |
340 | } |
341 | |
fc8628e3 |
342 | # general does() method |
53875581 |
343 | sub does :method; |
344 | *does = \&does_role; # alias |
345 | |
4093c859 |
346 | 1; |
347 | |
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 | |
034587d8 |
356 | This document describes Mouse version 0.40 |
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 | |
402 | =head1 UTILITIES FOR MOUSE |
403 | |
bedd575c |
404 | =head3 C<not_supported> |
f38ce2d0 |
405 | |
1820fffe |
406 | =head1 SEE ALSO |
407 | |
408 | L<Moose::Util> |
409 | |
5164490d |
410 | L<Class::MOP> |
1820fffe |
411 | |
412 | L<Sub::Identify> |
413 | |
414 | L<MRO::Compat> |
415 | |
f38ce2d0 |
416 | =cut |
417 | |