Add the script I've been using to munge tests to run against Moose
[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) {
272a1930 17 $impl = \&mro::get_linear_isa;
18 } else {
f172d4e7 19 my $loaded = do {
20 local $SIG{__DIE__} = 'DEFAULT';
bcd39bf4 21 eval "require MRO::Compat; 1";
f172d4e7 22 };
272a1930 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;
eae80759 47 }
48 }
bcd39bf4 49
272a1930 50 no strict 'refs';
51 *{ __PACKAGE__ . '::get_linear_isa'} = $impl;
eae80759 52}
53
2e92bb89 54sub apply_all_roles {
55 my $meta = Mouse::Meta::Class->initialize(shift);
2e92bb89 56
21498b08 57 my @roles;
f6715552 58
59 # Basis of Data::OptList
21498b08 60 my $max = scalar(@_);
61 for (my $i = 0; $i < $max ; $i++) {
62 if ($i + 1 < $max && ref($_[$i + 1])) {
63 push @roles, [ $_[$i++] => $_[$i] ];
64 } else {
65 push @roles, [ $_[$i] => {} ];
66 }
67 }
68
69 foreach my $role_spec (@roles) {
70 Mouse::load_class( $role_spec->[0] );
71 }
72
73 ( $_->[0]->can('meta') && $_->[0]->meta->isa('Mouse::Meta::Role') )
74 || croak("You can only consume roles, "
75 . $_->[0]
76 . " is not a Moose role")
77 foreach @roles;
78
79 if ( scalar @roles == 1 ) {
80 my ( $role, $params ) = @{ $roles[0] };
81 $role->meta->apply( $meta, ( defined $params ? %$params : () ) );
82 }
83 else {
84 Mouse::Meta::Role->combine_apply($meta, @roles);
85 }
86
2e92bb89 87}
88
4093c859 891;
90
f38ce2d0 91__END__
92
93=head1 NAME
94
95Mouse::Util - features, with or without their dependencies
96
97=head1 IMPLEMENTATIONS FOR
98
99=head2 L<MRO::Compat>
100
101=head3 get_linear_isa
102
f38ce2d0 103=cut
104