From: Dave Rolsky Date: Fri, 8 Aug 2008 16:51:38 +0000 (+0000) Subject: Make Moose::Util::TypeConstraints use Moose::Exporter too. X-Git-Tag: 0_55_01~43^2~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=23af33ae9f5f23ded09554c9db7e998e17e701a4;p=gitmo%2FMoose.git Make Moose::Util::TypeConstraints use Moose::Exporter too. Added an undocument _export_to_main param to Moose::Exporter's API, for the benefit of M::U::TC, which _does_ export to main, unlike Moose & Moose::Role. --- diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index 3b90584..abb0b81 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -30,7 +30,10 @@ sub build_import_methods { } ); - my $import = $class->_make_import_sub( $exporter, \@exports_from ); + # $args{_export_to_main} exists for backwards compat, because + # Moose::Util::TypeConstraints did export to main (unlike Moose & + # Moose::Role). + my $import = $class->_make_import_sub( $exporter, \@exports_from, $args{_export_to_main} ); my $unimport = $class->_make_unimport_sub( \@exports_from, [ keys %{$exports} ] ); @@ -152,8 +155,9 @@ sub _process_exports { sub _make_import_sub { shift; - my $exporter = shift; - my $exports_from = shift; + my $exporter = shift; + my $exports_from = shift; + my $export_to_main = shift; return sub { @@ -173,7 +177,7 @@ sub _process_exports { warnings->import; # we should never export to main - if ( $CALLER eq 'main' ) { + if ( $CALLER eq 'main' && ! $export_to_main ) { warn qq{$class does not export its sugar to the 'main' package.\n}; return; diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index ee579e8..59fb475 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -6,7 +6,7 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed'; -use Sub::Exporter; +use Moose::Exporter; our $VERSION = '0.56'; our $AUTHORITY = 'cpan:STEVAN'; @@ -62,38 +62,17 @@ use Moose::Meta::TypeCoercion::Union; use Moose::Meta::TypeConstraint::Registry; use Moose::Util::TypeConstraints::OptimizedConstraints; -my @exports = qw/ - type subtype class_type role_type as where message optimize_as - coerce from via - enum - find_type_constraint - register_type_constraint -/; - -Sub::Exporter::setup_exporter({ - exports => \@exports, - groups => { default => [':all'] } -}); - -sub unimport { - no strict 'refs'; - my $class = caller(); - # loop through the exports ... - foreach my $name (@exports) { - # if we find one ... - if (defined &{$class . '::' . $name}) { - my $keyword = \&{$class . '::' . $name}; - - # make sure it is from Moose - my ($pkg_name) = Class::MOP::get_code_info($keyword); - next if $@; - next if $pkg_name ne 'Moose::Util::TypeConstraints'; - - # and if it is from Moose then undef the slot - delete ${$class . '::'}{$name}; - } - } -} +Moose::Exporter->build_import_methods( + as_is => [ + qw( + type subtype class_type role_type as where message optimize_as + coerce from via + enum + find_type_constraint + register_type_constraint ) + ], + _export_to_main => 1, +); ## -------------------------------------------------------- ## type registry and some useful functions for it