}
);
- 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} ] );
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 {
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;
use Carp 'confess';
use Scalar::Util 'blessed';
-use Sub::Exporter;
+use Moose::Exporter;
our $VERSION = '0.56';
our $AUTHORITY = 'cpan:STEVAN';
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