my %EXPORT_SPEC;
+sub setup_import_methods {
+ my ( $class, %args ) = @_;
+
+ my $exporting_package = $args{exporting_package} ||= caller();
+
+ my ( $import, $unimport ) = $class->build_import_methods(%args);
+
+ no strict 'refs';
+ *{ $exporting_package . '::import' } = $import;
+ *{ $exporting_package . '::unimport' } = $unimport;
+}
+
sub build_import_methods {
- my $class = shift;
- my %args = @_;
+ my ( $class, %args ) = @_;
- my $exporting_package = caller();
+ my $exporting_package = $args{exporting_package} ||= caller();
$EXPORT_SPEC{$exporting_package} = \%args;
# $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 $import = $class->_make_import_sub( $exporting_package, $exporter,
+ \@exports_from, $args{_export_to_main} );
- my $unimport = $class->_make_unimport_sub( \@exports_from, [ keys %{$exports} ] );
+ my $unimport
+ = $class->_make_unimport_sub( $exporting_package, \@exports_from,
+ [ keys %{$exports} ] );
- no strict 'refs';
- *{ $exporting_package . '::import' } = $import;
- *{ $exporting_package . '::unimport' } = $unimport;
+ return ( $import, $unimport )
}
{
- my %seen;
+ my $seen = {};
sub _follow_also {
my $class = shift;
my $exporting_package = shift;
- %seen = ( $exporting_package => 1 );
+ local %$seen = ( $exporting_package => 1 );
return uniq( _follow_also_real($exporting_package) );
}
for my $package (@also)
{
die "Circular reference in also parameter to MooseX::Exporter between $exporting_package and $package"
- if $seen{$package};
+ if $seen->{$package};
- $seen{$package} = 1;
+ $seen->{$package} = 1;
}
return @also, map { _follow_also_real($_) } @also;
sub _make_import_sub {
shift;
- my $exporter = shift;
- my $exports_from = shift;
- my $export_to_main = shift;
+ my $exporting_package = shift;
+ my $exporter = shift;
+ my $exports_from = shift;
+ my $export_to_main = shift;
return sub {
# I think we could use Sub::Exporter's collector feature
my $traits;
($traits, @_) = Moose::Exporter::_strip_traits(@_);
- # It's important to leave @_ as-is for the benefit of
- # Sub::Exporter.
- my $class = $_[0];
+ # Normally we could look at $_[0], but in some weird cases
+ # (involving goto &Moose::import), $_[0] ends as something
+ # else (like Squirrel).
+ my $class = $exporting_package;
$CALLER = Moose::Exporter::_get_caller(@_);
sub _make_unimport_sub {
shift;
- my $sources = shift;
- my $keywords = shift;
+ my $exporting_package = shift;
+ my $sources = shift;
+ my $keywords = shift;
return sub {
- my $class = shift;
my $caller = scalar caller();
Moose::Exporter->_remove_keywords(
$caller,
- [ $class, @{$sources} ],
+ [ $exporting_package, @{$sources} ],
$keywords
);
};
use Moose ();
use Moose::Exporter;
- Moose::Exporter->build_export_methods(
- export => [ 'sugar1', 'sugar2', \&Some::Random::thing ],
- init_meta_args => { metaclass_class => 'MyApp::Meta::Class' ],
+ Moose::Exporter->setup_import_methods(
+ with_caller => [ 'sugar1', 'sugar2' ],
+ as_is => [ 'sugar3', \&Some::Random::thing ],
+ also => 'Moose',
);
# then later ...
=head1 METHODS
-This module provides exactly one public method:
+This module provides two public methods:
-=head2 Moose::Exporter->build_import_methods(...)
+=head2 Moose::Exporter->setup_import_methods(...)
When you call this method, C<Moose::Exporter> build custom C<import>
and C<unimport> methods for your module. The import method will export
=back
+=head2 Moose::Exporter->build_import_methods(...)
+
+Returns two code refs, one for import and one for unimport.
+
+Used by C<setup_import_methods>.
+
=head1 IMPORTING AND init_meta
If you want to set an alternative base object class or metaclass
use Moose -traits => [ 'My::Meta::Trait', 'My::Other::Trait' ];
-These traits will be applied to the caller's metaclass instance. These traits will be ignored
+These traits will be applied to the caller's metaclass
+instance. Providing traits for an exporting class that does not create
+a metaclass for the caller is an error.
=head1 AUTHOR