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