2 use Mouse::Exporter; # enables strict and warnings
4 sub get_linear_isa($;$); # must be here
7 # This is used in Mouse::PurePerl
8 Mouse::Exporter->setup_import_methods(
12 resolve_metaclass_alias
30 default => [], # export no functions by default
32 # The ':meta' group is 'use metaclass' for Mouse
33 meta => [qw(does meta dump)],
38 # Because Mouse::Util is loaded first in all the Mouse sub-modules,
39 # XS loader is placed here, not in Mouse.pm.
41 our $VERSION = '0.47';
43 my $xs = !(exists $INC{'Mouse/PurePerl.pm'} || $ENV{MOUSE_PUREPERL});
46 # XXX: XSLoader tries to get the object path from caller's file name
47 # $hack_mouse_file fools its mechanism
49 (my $hack_mouse_file = __FILE__) =~ s/.Util//; # .../Mouse/Util.pm -> .../Mouse.pm
50 $xs = eval sprintf("#line %d %s\n", __LINE__, $hack_mouse_file) . q{
52 XSLoader::load('Mouse', $VERSION);
53 Mouse::Util->import({ into => 'Mouse::Meta::Method::Constructor::XS' }, ':meta');
54 Mouse::Util->import({ into => 'Mouse::Meta::Method::Destructor::XS' }, ':meta');
55 Mouse::Util->import({ into => 'Mouse::Meta::Method::Accessor::XS' }, ':meta');
62 require 'Mouse/PurePerl.pm'; # we don't want to create its namespace
65 *MOUSE_XS = sub(){ $xs };
71 use constant _MOUSE_VERBOSE => !!$ENV{MOUSE_VERBOSE};
73 # aliases as public APIs
74 # it must be 'require', not 'use', because Mouse::Meta::Module depends on Mouse::Util
75 require Mouse::Meta::Module; # for the entities of metaclass cache utilities
78 *class_of = \&Mouse::Meta::Module::_class_of;
79 *get_metaclass_by_name = \&Mouse::Meta::Module::_get_metaclass_by_name;
80 *get_all_metaclass_instances = \&Mouse::Meta::Module::_get_all_metaclass_instances;
81 *get_all_metaclass_names = \&Mouse::Meta::Module::_get_all_metaclass_names;
84 #generate_isa_predicate_for('Mouse::Meta::TypeConstraint' => 'is_a_type_constraint');
85 #generate_isa_predicate_for('Mouse::Meta::Class' => 'is_a_metaclass');
86 #generate_isa_predicate_for('Mouse::Meta::Role' => 'is_a_metarole');
88 # duck type predicates
89 generate_can_predicate_for(['_compiled_type_constraint'] => 'is_a_type_constraint');
90 generate_can_predicate_for(['create_anon_class'] => 'is_a_metaclass');
91 generate_can_predicate_for(['create_anon_role'] => 'is_a_metarole');
94 our $in_global_destruction = 0;
95 END{ $in_global_destruction = 1 }
97 # Moose::Util compatible utilities
100 return class_of( $_[0] );
104 my ($class_or_obj, $role_name) = @_;
106 my $meta = class_of($class_or_obj);
109 || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()");
111 return defined($meta) && $meta->does_role($role_name);
116 if ($] >= 5.009_005) {
118 $get_linear_isa = \&mro::get_linear_isa;
120 # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
121 my $_get_linear_isa_dfs; # this recurses so it isn't pretty
122 $_get_linear_isa_dfs = sub {
125 my @lin = ($classname);
129 foreach my $parent (@{"$classname\::ISA"}) {
130 my $plin = $_get_linear_isa_dfs->($parent);
131 foreach my $p(@$plin) {
132 next if exists $stored{$p};
139 # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
141 eval{ require Class::C3 };
143 # MRO::Compat::__get_linear_isa has no prototype, so
144 # we define a prototyped version for compatibility with core's
145 # See also MRO::Compat::__get_linear_isa.
146 $get_linear_isa = sub ($;$){
147 my($classname, $type) = @_;
148 package # hide from PAUSE
152 $type = exists $MRO{$classname} ? 'c3' : 'dfs';
155 ? [calculateMRO($classname)]
156 : $_get_linear_isa_dfs->($classname);
160 *get_linear_isa = $get_linear_isa;
164 # taken from Mouse::Util (0.90)
168 sub resolve_metaclass_alias {
169 my ( $type, $metaclass_name, %options ) = @_;
171 my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' );
173 return $cache{$cache_key}{$metaclass_name} ||= do{
175 my $possible_full_name = join '::',
176 'Mouse::Meta', $type, 'Custom', ($options{trait} ? 'Trait' : ()), $metaclass_name
179 my $loaded_class = load_first_existing_class(
184 $loaded_class->can('register_implementation')
185 ? $loaded_class->register_implementation
191 # Utilities from Class::MOP
194 sub get_code_package;
196 # taken from Class/MOP.pm
197 sub is_valid_class_name {
200 return 0 if ref($class);
201 return 0 unless defined($class);
203 return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms;
208 # taken from Class/MOP.pm
209 sub load_first_existing_class {
214 for my $class (@classes) {
215 my $e = _try_load_one_class($class);
218 $exceptions{$class} = $e;
229 sprintf( "Could not load class (%s) because : %s",
230 $_, $exceptions{$_} )
235 # taken from Class/MOP.pm
236 my %is_class_loaded_cache;
237 sub _try_load_one_class {
240 unless ( is_valid_class_name($class) ) {
241 my $display = defined($class) ? $class : 'undef';
242 Carp::confess "Invalid class name ($display)";
245 return undef if $is_class_loaded_cache{$class} ||= is_class_loaded($class);
247 my $file = $class . '.pm';
252 eval { require($file) };
260 my $e = _try_load_one_class($class);
261 Carp::confess "Could not load class ($class) because : $e" if $e;
268 sub apply_all_roles {
269 my $applicant = Scalar::Util::blessed($_[0])
271 : Mouse::Meta::Class->initialize(shift); # class or role name
275 # Basis of Data::OptList
276 my $max = scalar(@_);
277 for (my $i = 0; $i < $max ; $i++) {
278 if ($i + 1 < $max && ref($_[$i + 1])) {
279 push @roles, [ $_[$i] => $_[++$i] ];
281 push @roles, [ $_[$i] => undef ];
283 my $role_name = $roles[-1][0];
284 load_class($role_name);
286 is_a_metarole( get_metaclass_by_name($role_name) )
287 || $applicant->meta->throw_error("You can only consume roles, $role_name is not a Mouse role");
290 if ( scalar @roles == 1 ) {
291 my ( $role_name, $params ) = @{ $roles[0] };
292 get_metaclass_by_name($role_name)->apply( $applicant, defined $params ? $params : () );
295 Mouse::Meta::Role->combine(@roles)->apply($applicant);
300 # taken from Moose::Util 0.90
302 return $_[0] if @_ == 1;
306 return "$items[0] and $items[1]" if @items == 2;
308 my $tail = pop @items;
310 return join q{, }, @items, "and $tail";
318 $feature ||= ( caller(1) )[3]; # subroutine name
320 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
321 Carp::confess("Mouse does not currently support $feature");
324 # general meta() method
326 return Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]);
329 # general dump() method
331 my($self, $maxdepth) = @_;
333 require 'Data/Dumper.pm'; # we don't want to create its namespace
334 my $dd = Data::Dumper->new([$self]);
335 $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 2);
340 # general does() method
342 *does = \&does_role; # alias
349 Mouse::Util - Features, with or without their dependencies
353 This document describes Mouse version 0.47
355 =head1 IMPLEMENTATIONS FOR
363 =head3 C<resolve_metaclass_alias>
365 =head3 C<apply_all_roles>
367 =head3 C<english_list>
371 =head3 C<< is_class_loaded(ClassName) -> Bool >>
373 Returns whether C<ClassName> is actually loaded or not. It uses a heuristic which
374 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
375 locally-defined method.
377 =head3 C<< load_class(ClassName) >>
379 This will load a given C<ClassName> (or die if it is not loadable).
380 This function can be used in place of tricks like
381 C<eval "use $module"> or using C<require>.
383 =head3 C<< Mouse::Util::class_of(ClassName or Object) >>
385 =head3 C<< Mouse::Util::get_metaclass_by_name(ClassName) >>
387 =head3 C<< Mouse::Util::get_all_metaclass_instances() >>
389 =head3 C<< Mouse::Util::get_all_metaclass_names() >>
393 =head3 C<get_linear_isa>
397 =head3 C<get_code_info>
399 =head1 Mouse specific utilities
401 =head3 C<not_supported>
403 =head3 C<get_code_package>
405 =head3 C<get_code_ref>