From: Florian Ragwitz Date: Mon, 30 Nov 2009 14:54:20 +0000 (+0100) Subject: Warn when Moose::Exporter is overwriting existing symbols. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5f4c8b404ec4035ced1c5dc4bef024138ea7aefb;p=gitmo%2FMoose.git Warn when Moose::Exporter is overwriting existing symbols. --- diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index 9d3b785..f33711f 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -9,7 +9,9 @@ $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; use Class::MOP; +use Carp qw(carp); use List::MoreUtils qw( first_index uniq ); +use Moose::Util; use Moose::Util::MetaRole; use Sub::Exporter 0.980; use Sub::Name qw(subname); @@ -51,8 +53,30 @@ sub build_import_methods { my $exporter = Sub::Exporter::build_exporter( { - exports => $exports, - groups => { default => [':all'] } + exports => $exports, + groups => { default => [':all'] }, + installer => sub { + my ($arg, $to_export) = @_; + my $meta = Class::MOP::Package->initialize($arg->{into}); + + my @overwritten; + + for (my $i = 0; $i < @{ $to_export }; $i += 2) { + my $as = $to_export->[$i]; + push @overwritten, $as + if $meta->has_package_symbol('&' . $as); + } + + if (@overwritten) { + local $Carp::CarpLevel = 2; + carp $arg->{class} + . " is overwriting symbol" + . (@overwritten > 1 ? 's' : '') . " " + . Moose::Util::english_list(map { join '::' => $arg->{into}, $_ } @overwritten); + } + + goto &Sub::Exporter::default_installer; + }, } ); diff --git a/t/050_metaclasses/012_moose_exporter.t b/t/050_metaclasses/012_moose_exporter.t index 2809867..2c4fe79 100644 --- a/t/050_metaclasses/012_moose_exporter.t +++ b/t/050_metaclasses/012_moose_exporter.t @@ -393,10 +393,11 @@ BEGIN { package AlreadyHasImport; sub as_is1 { 42 } + sub with_caller1 { 23 } ::stderr_like( - sub { AllOptions->import('as_is1') }, - qr/AllOptions is overwriting symbol as_is1 at ${\__FILE__}/, + sub { AllOptions->import('as_is1', 'with_caller1') }, + qr/AllOptions is overwriting symbols AlreadyHasImport::as_is1 and AlreadyHasImport::with_caller1 at ${\__FILE__}/, 'warning when overwriting existign symbols', );