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