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