Doh, the SYNOPSIS was totally wrong.
[gitmo/Moose.git] / lib / Moose / Exporter.pm
index 571c9b2..9d4d64c 100644 (file)
@@ -11,11 +11,22 @@ use Sub::Exporter;
 
 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;
 
@@ -34,23 +45,24 @@ sub build_import_methods {
     # $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) );
     }
@@ -70,9 +82,9 @@ sub build_import_methods {
         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;
@@ -156,9 +168,10 @@ sub _make_sub_exporter_params {
 
     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
@@ -172,9 +185,10 @@ sub _make_sub_exporter_params {
             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(@_);
 
@@ -272,15 +286,15 @@ sub _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
         );
     };
@@ -331,9 +345,10 @@ Moose::Exporter - make an import() and unimport() just like Moose.pm
   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 ...
@@ -359,9 +374,9 @@ C<MooseX> module, as long as they all use C<Moose::Exporter>.
 
 =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
@@ -400,6 +415,12 @@ when C<unimport> is called.
 
 =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
@@ -426,7 +447,9 @@ parameter passed as part of the import:
 
   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