From: Toby Inkster Date: Sun, 9 Dec 2012 09:37:34 +0000 (+0000) Subject: die if somebody imports both Moo::Role and Moo into a package X-Git-Tag: v1.000007~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1791ba325ca381778870f7e4927eeca4b3ed1cf1;p=gitmo%2FMoo.git die if somebody imports both Moo::Role and Moo into a package --- diff --git a/Changes b/Changes index c620ed8..fa108f3 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,7 @@ + + + - Die if Moo and Moo::Role are imported into the same package + 1.000006 - 2012-11-16 - Don't use $_ as loop variable when calling arbitrary code (RT#81072) - Bump Role::Tiny prereq to fix method modifier breakage on 5.10.0 diff --git a/lib/Moo.pm b/lib/Moo.pm index 962ff05..867869b 100644 --- a/lib/Moo.pm +++ b/lib/Moo.pm @@ -22,8 +22,11 @@ sub import { my $target = caller; my $class = shift; strictures->import; + if ($Moo::Role::INFO{$target} and $Moo::Role::INFO{$target}{is_role}) { + die "Cannot import Moo into a role"; + } return if $MAKERS{$target}; # already exported into this package - $MAKERS{$target} = {}; + $MAKERS{$target} = { is_class => 1 }; _install_tracked $target => extends => sub { $class->_set_superclasses($target, @_); $class->_maybe_reset_handlemoose($target); diff --git a/lib/Moo/Role.pm b/lib/Moo/Role.pm index 61d66da..f823754 100644 --- a/lib/Moo/Role.pm +++ b/lib/Moo/Role.pm @@ -20,8 +20,11 @@ sub import { my $target = caller; my ($me) = @_; strictures->import; + if ($Moo::MAKERS{$target} and $Moo::MAKERS{$target}{is_class}) { + die "Cannot import Moo::Role into a Moo class"; + } return if $INFO{$target}; # already exported into this package - $INFO{$target} = {}; + $INFO{$target} = { is_role => 1 }; # get symbol table reference my $stash = do { no strict 'refs'; \%{"${target}::"} }; _install_tracked $target => has => sub { diff --git a/t/not-both.t b/t/not-both.t new file mode 100644 index 0000000..8236490 --- /dev/null +++ b/t/not-both.t @@ -0,0 +1,17 @@ +use strictures 1; +use Test::More; + +# Compile-time exceptions, so need stringy eval; hence not Test::Fatal. +{ + local $@; + ok not eval q { package XXX; use Moo; use Moo::Role; 1; }; + like $@, qr{Cannot import Moo::Role into a Moo class}; +} + +{ + local $@; + ok not eval q { package YYY; use Moo::Role; use Moo; 1; }; + like $@, qr{Cannot import Moo into a role}; +} + +done_testing;