Load mro directly if Perl is recent enough (Nicholas Clark)
[gitmo/Mouse.git] / lib / Mouse / Util.pm
CommitLineData
4093c859 1package Mouse::Util;
2use strict;
3use warnings;
5c12655d 4use base qw/Exporter/;
d72006ca 5use Carp;
4093c859 6
eae80759 7our @EXPORT_OK = qw(
eae80759 8 get_linear_isa
eae80759 9);
10our %EXPORT_TAGS = (
11 all => \@EXPORT_OK,
12);
13
42d7df00 14BEGIN {
272a1930 15 my $impl;
bcd39bf4 16 if ($] >= 5.009_005) {
388b8ebd 17 require mro;
272a1930 18 $impl = \&mro::get_linear_isa;
19 } else {
f172d4e7 20 my $loaded = do {
21 local $SIG{__DIE__} = 'DEFAULT';
bcd39bf4 22 eval "require MRO::Compat; 1";
f172d4e7 23 };
272a1930 24 if ($loaded) {
25 $impl = \&mro::get_linear_isa;
26 } else {
27# VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
28 my $code; # this recurses so it isn't pretty
29 $code = sub {
30 no strict 'refs';
31
32 my $classname = shift;
33
34 my @lin = ($classname);
35 my %stored;
36 foreach my $parent (@{"$classname\::ISA"}) {
37 my $plin = $code->($parent);
38 foreach (@$plin) {
39 next if exists $stored{$_};
40 push(@lin, $_);
41 $stored{$_} = 1;
42 }
43 }
44 return \@lin;
45 };
46# ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
47 $impl = $code;
eae80759 48 }
49 }
bcd39bf4 50
272a1930 51 no strict 'refs';
52 *{ __PACKAGE__ . '::get_linear_isa'} = $impl;
eae80759 53}
54
2e92bb89 55sub apply_all_roles {
56 my $meta = Mouse::Meta::Class->initialize(shift);
2e92bb89 57
21498b08 58 my @roles;
f6715552 59
60 # Basis of Data::OptList
21498b08 61 my $max = scalar(@_);
62 for (my $i = 0; $i < $max ; $i++) {
63 if ($i + 1 < $max && ref($_[$i + 1])) {
64 push @roles, [ $_[$i++] => $_[$i] ];
65 } else {
66 push @roles, [ $_[$i] => {} ];
67 }
68 }
69
70 foreach my $role_spec (@roles) {
71 Mouse::load_class( $role_spec->[0] );
72 }
73
74 ( $_->[0]->can('meta') && $_->[0]->meta->isa('Mouse::Meta::Role') )
75 || croak("You can only consume roles, "
76 . $_->[0]
77 . " is not a Moose role")
78 foreach @roles;
79
80 if ( scalar @roles == 1 ) {
81 my ( $role, $params ) = @{ $roles[0] };
82 $role->meta->apply( $meta, ( defined $params ? %$params : () ) );
83 }
84 else {
85 Mouse::Meta::Role->combine_apply($meta, @roles);
86 }
87
2e92bb89 88}
89
4093c859 901;
91
f38ce2d0 92__END__
93
94=head1 NAME
95
96Mouse::Util - features, with or without their dependencies
97
98=head1 IMPLEMENTATIONS FOR
99
100=head2 L<MRO::Compat>
101
102=head3 get_linear_isa
103
f38ce2d0 104=cut
105