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