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