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