Rename meta_generator to meta_lookup
[gitmo/Moose.git] / lib / Moose / Exporter.pm
1 package Moose::Exporter;
2
3 use strict;
4 use warnings;
5
6 use Class::MOP;
7 use List::MoreUtils qw( first_index uniq );
8 use Moose::Util::MetaRole;
9 use Scalar::Util qw(reftype);
10 use Sub::Exporter 0.980;
11 use Sub::Name qw(subname);
12
13 my %EXPORT_SPEC;
14
15 sub setup_import_methods {
16     my ( $class, %args ) = @_;
17
18     $args{exporting_package} ||= caller();
19
20     $class->build_import_methods(
21         %args,
22         install => [qw(import unimport init_meta)]
23     );
24 }
25
26 sub build_import_methods {
27     my ( $class, %args ) = @_;
28
29     my $exporting_package = $args{exporting_package} ||= caller();
30
31     my $meta_lookup = $args{meta_lookup} || sub { Class::MOP::class_of(shift) };
32
33     $EXPORT_SPEC{$exporting_package} = \%args;
34
35     my @exports_from = $class->_follow_also($exporting_package);
36
37     my $export_recorder = {};
38     my $is_reexport     = {};
39
40     my $exports = $class->_make_sub_exporter_params(
41         [ @exports_from, $exporting_package ],
42         $export_recorder,
43         $is_reexport,
44         $meta_lookup,
45     );
46
47     my $exporter = $class->_make_exporter(
48         $exports,
49         $is_reexport,
50         $meta_lookup,
51     );
52
53     my %methods;
54     $methods{import} = $class->_make_import_sub(
55         $exporting_package,
56         $exporter,
57         \@exports_from,
58         $is_reexport,
59         $meta_lookup,
60     );
61
62     $methods{unimport} = $class->_make_unimport_sub(
63         $exporting_package,
64         $exports,
65         $export_recorder,
66         $is_reexport,
67         $meta_lookup,
68     );
69
70     $methods{init_meta} = $class->_make_init_meta(
71         $exporting_package,
72         \%args,
73         $meta_lookup,
74     );
75
76     my $package = Class::MOP::Package->initialize($exporting_package);
77     for my $to_install ( @{ $args{install} || [] } ) {
78         my $symbol = '&' . $to_install;
79         next
80             unless $methods{$to_install}
81                 && !$package->has_package_symbol($symbol);
82         $package->add_package_symbol( $symbol, $methods{$to_install} );
83     }
84
85     return ( $methods{import}, $methods{unimport}, $methods{init_meta} );
86 }
87
88 sub _make_exporter {
89     my ($class, $exports, $is_reexport, $meta_lookup) = @_;
90
91     return Sub::Exporter::build_exporter(
92         {
93             exports   => $exports,
94             groups    => { default => [':all'] },
95             installer => sub {
96                 my ($arg, $to_export) = @_;
97                 my $meta = $meta_lookup->($arg->{into});
98
99                 goto &Sub::Exporter::default_installer unless $meta;
100
101                 # don't overwrite existing symbols with our magically flagged
102                 # version of it if we would install the same sub that's already
103                 # in the importer
104
105                 my @filtered_to_export;
106                 my %installed;
107                 for (my $i = 0; $i < @{ $to_export }; $i += 2) {
108                     my ($as, $cv) = @{ $to_export }[$i, $i + 1];
109
110                     next if !ref($as)
111                          && $meta->has_package_symbol('&' . $as)
112                          && $meta->get_package_symbol('&' . $as) == $cv;
113
114                     push @filtered_to_export, $as, $cv;
115                     $installed{$as} = 1 unless ref $as;
116                 }
117
118                 Sub::Exporter::default_installer($arg, \@filtered_to_export);
119
120                 for my $name ( keys %{$is_reexport} ) {
121                     no strict 'refs';
122                     no warnings 'once';
123                     next unless exists $installed{$name};
124                     _flag_as_reexport( \*{ join q{::}, $arg->{into}, $name } );
125                 }
126             },
127         }
128     );
129 }
130
131 {
132     my $seen = {};
133
134     sub _follow_also {
135         my $class             = shift;
136         my $exporting_package = shift;
137
138         local %$seen = ( $exporting_package => 1 );
139
140         return reverse uniq( _follow_also_real($exporting_package) );
141     }
142
143     sub _follow_also_real {
144         my $exporting_package = shift;
145
146         if ( !exists $EXPORT_SPEC{$exporting_package} ) {
147             my $loaded = Class::MOP::is_class_loaded($exporting_package);
148
149             die "Package in also ($exporting_package) does not seem to "
150                 . "use Moose::Exporter"
151                 . ( $loaded ? "" : " (is it loaded?)" );
152         }
153
154         my $also = $EXPORT_SPEC{$exporting_package}{also};
155
156         return unless defined $also;
157
158         my @also = ref $also ? @{$also} : $also;
159
160         for my $package (@also) {
161             die
162                 "Circular reference in 'also' parameter to Moose::Exporter between $exporting_package and $package"
163                 if $seen->{$package};
164
165             $seen->{$package} = 1;
166         }
167
168         return @also, map { _follow_also_real($_) } @also;
169     }
170 }
171
172 sub _parse_trait_aliases {
173     my $class   = shift;
174     my ($package, $aliases) = @_;
175
176     my @ret;
177     for my $alias (@$aliases) {
178         my $name;
179         if (ref($alias)) {
180             reftype($alias) eq 'ARRAY'
181                 or Moose->throw_error(reftype($alias) . " references are not "
182                                     . "valid arguments to the 'trait_aliases' "
183                                     . "option");
184
185             ($alias, $name) = @$alias;
186         }
187         else {
188             ($name = $alias) =~ s/.*:://;
189         }
190         push @ret, subname "${package}::${name}" => sub () { $alias };
191     }
192
193     return @ret;
194 }
195
196 sub _make_sub_exporter_params {
197     my $class           = shift;
198     my $packages        = shift;
199     my $export_recorder = shift;
200     my $is_reexport     = shift;
201     my $meta_lookup     = shift;
202
203     my %exports;
204
205     for my $package ( @{$packages} ) {
206         my $args = $EXPORT_SPEC{$package}
207             or die "The $package package does not use Moose::Exporter\n";
208
209         for my $name ( @{ $args->{with_meta} } ) {
210             my $sub = $class->_sub_from_package( $package, $name )
211                 or next;
212
213             my $fq_name = $package . '::' . $name;
214
215             $exports{$name} = $class->_make_wrapped_sub_with_meta(
216                 $fq_name,
217                 $sub,
218                 $export_recorder,
219                 $meta_lookup,
220             );
221         }
222
223         for my $name ( @{ $args->{with_caller} } ) {
224             my $sub = $class->_sub_from_package( $package, $name )
225                 or next;
226
227             my $fq_name = $package . '::' . $name;
228
229             $exports{$name} = $class->_make_wrapped_sub(
230                 $fq_name,
231                 $sub,
232                 $export_recorder,
233             );
234         }
235
236         my @extra_exports = $class->_parse_trait_aliases(
237             $package, $args->{trait_aliases},
238         );
239         for my $name ( @{ $args->{as_is} }, @extra_exports ) {
240             my ( $sub, $coderef_name );
241
242             if ( ref $name ) {
243                 $sub = $name;
244
245                 my $coderef_pkg;
246                 ( $coderef_pkg, $coderef_name )
247                     = Class::MOP::get_code_info($name);
248
249                 if ( $coderef_pkg ne $package ) {
250                     $is_reexport->{$coderef_name} = 1;
251                 }
252             }
253             else {
254                 $sub = $class->_sub_from_package( $package, $name )
255                     or next;
256
257                 $coderef_name = $name;
258             }
259
260             $export_recorder->{$sub} = 1;
261
262             $exports{$coderef_name} = sub {$sub};
263         }
264     }
265
266     return \%exports;
267 }
268
269 sub _sub_from_package {
270     my $sclass  = shift;
271     my $package = shift;
272     my $name    = shift;
273
274     my $sub = do {
275         no strict 'refs';
276         \&{ $package . '::' . $name };
277     };
278
279     return $sub if defined &$sub;
280
281     Carp::cluck "Trying to export undefined sub ${package}::${name}";
282
283     return;
284 }
285
286 our $CALLER;
287
288 sub _make_wrapped_sub {
289     my $self            = shift;
290     my $fq_name         = shift;
291     my $sub             = shift;
292     my $export_recorder = shift;
293
294     # We need to set the package at import time, so that when
295     # package Foo imports has(), we capture "Foo" as the
296     # package. This lets other packages call Foo::has() and get
297     # the right package. This is done for backwards compatibility
298     # with existing production code, not because this is a good
299     # idea ;)
300     return sub {
301         my $caller = $CALLER;
302
303         my $wrapper = $self->_curry_wrapper( $sub, $fq_name, $caller );
304
305         my $sub = subname( $fq_name => $wrapper );
306
307         $export_recorder->{$sub} = 1;
308
309         return $sub;
310     };
311 }
312
313 sub _make_wrapped_sub_with_meta {
314     my $self            = shift;
315     my $fq_name         = shift;
316     my $sub             = shift;
317     my $export_recorder = shift;
318     my $meta_lookup     = shift;
319
320     return sub {
321         my $caller = $CALLER;
322
323         my $wrapper = $self->_late_curry_wrapper(
324             $sub, $fq_name,
325             $meta_lookup => $caller
326         );
327
328         my $sub = subname( $fq_name => $wrapper );
329
330         $export_recorder->{$sub} = 1;
331
332         return $sub;
333     };
334 }
335
336 sub _curry_wrapper {
337     my $class   = shift;
338     my $sub     = shift;
339     my $fq_name = shift;
340     my @extra   = @_;
341
342     my $wrapper = sub { $sub->( @extra, @_ ) };
343     if ( my $proto = prototype $sub ) {
344
345         # XXX - Perl's prototype sucks. Use & to make set_prototype
346         # ignore the fact that we're passing "private variables"
347         &Scalar::Util::set_prototype( $wrapper, $proto );
348     }
349     return $wrapper;
350 }
351
352 sub _late_curry_wrapper {
353     my $class   = shift;
354     my $sub     = shift;
355     my $fq_name = shift;
356     my $extra   = shift;
357     my @ex_args = @_;
358
359     my $wrapper = sub {
360
361         # resolve curried arguments at runtime via this closure
362         my @curry = ( $extra->(@ex_args) );
363         return $sub->( @curry, @_ );
364     };
365
366     if ( my $proto = prototype $sub ) {
367
368         # XXX - Perl's prototype sucks. Use & to make set_prototype
369         # ignore the fact that we're passing "private variables"
370         &Scalar::Util::set_prototype( $wrapper, $proto );
371     }
372     return $wrapper;
373 }
374
375 sub _make_import_sub {
376     shift;
377     my $exporting_package = shift;
378     my $exporter          = shift;
379     my $exports_from      = shift;
380     my $is_reexport       = shift;
381     my $meta_lookup       = shift;
382
383     return sub {
384
385         # I think we could use Sub::Exporter's collector feature
386         # to do this, but that would be rather gross, since that
387         # feature isn't really designed to return a value to the
388         # caller of the exporter sub.
389         #
390         # Also, this makes sure we preserve backwards compat for
391         # _get_caller, so it always sees the arguments in the
392         # expected order.
393         my $traits;
394         ( $traits, @_ ) = _strip_traits(@_);
395
396         my $metaclass;
397         ( $metaclass, @_ ) = _strip_metaclass(@_);
398         $metaclass
399             = Moose::Util::resolve_metaclass_alias( 'Class' => $metaclass )
400             if defined $metaclass && length $metaclass;
401
402         my $meta_name;
403         ( $meta_name, @_ ) = _strip_meta_name(@_);
404
405         # Normally we could look at $_[0], but in some weird cases
406         # (involving goto &Moose::import), $_[0] ends as something
407         # else (like Squirrel).
408         my $class = $exporting_package;
409
410         $CALLER = _get_caller(@_);
411
412         # this works because both pragmas set $^H (see perldoc
413         # perlvar) which affects the current compilation -
414         # i.e. the file who use'd us - which is why we don't need
415         # to do anything special to make it affect that file
416         # rather than this one (which is already compiled)
417
418         strict->import;
419         warnings->import;
420
421         my $did_init_meta;
422         for my $c ( grep { $_->can('init_meta') } $class, @{$exports_from} ) {
423
424             # init_meta can apply a role, which when loaded uses
425             # Moose::Exporter, which in turn sets $CALLER, so we need
426             # to protect against that.
427             local $CALLER = $CALLER;
428             $c->init_meta(
429                 for_class => $CALLER,
430                 metaclass => $metaclass,
431                 meta_name => $meta_name,
432             );
433             $did_init_meta = 1;
434         }
435
436         if ( $did_init_meta && @{$traits} ) {
437
438             # The traits will use Moose::Role, which in turn uses
439             # Moose::Exporter, which in turn sets $CALLER, so we need
440             # to protect against that.
441             local $CALLER = $CALLER;
442             _apply_meta_traits( $CALLER, $traits, $meta_lookup );
443         }
444         elsif ( @{$traits} ) {
445             require Moose;
446             Moose->throw_error(
447                 "Cannot provide traits when $class does not have an init_meta() method"
448             );
449         }
450
451         my ( undef, @args ) = @_;
452         my $extra = shift @args if ref $args[0] eq 'HASH';
453
454         $extra ||= {};
455         if ( !$extra->{into} ) {
456             $extra->{into_level} ||= 0;
457             $extra->{into_level}++;
458         }
459
460         $class->$exporter( $extra, @args );
461     };
462 }
463
464 sub _strip_traits {
465     my $idx = first_index { ( $_ || '' ) eq '-traits' } @_;
466
467     return ( [], @_ ) unless $idx >= 0 && $#_ >= $idx + 1;
468
469     my $traits = $_[ $idx + 1 ];
470
471     splice @_, $idx, 2;
472
473     $traits = [$traits] unless ref $traits;
474
475     return ( $traits, @_ );
476 }
477
478 sub _strip_metaclass {
479     my $idx = first_index { ( $_ || '' ) eq '-metaclass' } @_;
480
481     return ( undef, @_ ) unless $idx >= 0 && $#_ >= $idx + 1;
482
483     my $metaclass = $_[ $idx + 1 ];
484
485     splice @_, $idx, 2;
486
487     return ( $metaclass, @_ );
488 }
489
490 sub _strip_meta_name {
491     my $idx = first_index { ( $_ || '' ) eq '-meta_name' } @_;
492
493     return ( 'meta', @_ ) unless $idx >= 0 && $#_ >= $idx + 1;
494
495     my $meta_name = $_[ $idx + 1 ];
496
497     splice @_, $idx, 2;
498
499     return ( $meta_name, @_ );
500 }
501
502 sub _apply_meta_traits {
503     my ( $class, $traits, $meta_lookup ) = @_;
504
505     return unless @{$traits};
506
507     my $meta = $meta_lookup->($class);
508
509     my $type = ( split /::/, ref $meta )[-1]
510         or Moose->throw_error(
511         'Cannot determine metaclass type for trait application . Meta isa '
512             . ref $meta );
513
514     my @resolved_traits = map {
515         ref $_
516             ? $_
517             : Moose::Util::resolve_metatrait_alias( $type => $_ )
518     } @$traits;
519
520     return unless @resolved_traits;
521
522     my %args = ( for => $class );
523
524     if ( $meta->isa('Moose::Meta::Role') ) {
525         $args{role_metaroles} = { role => \@resolved_traits };
526     }
527     else {
528         $args{class_metaroles} = { class => \@resolved_traits };
529     }
530
531     Moose::Util::MetaRole::apply_metaroles(%args);
532 }
533
534 sub _get_caller {
535
536     # 1 extra level because it's called by import so there's a layer
537     # of indirection
538     my $offset = 1;
539
540     return
541           ( ref $_[1] && defined $_[1]->{into} ) ? $_[1]->{into}
542         : ( ref $_[1] && defined $_[1]->{into_level} )
543         ? caller( $offset + $_[1]->{into_level} )
544         : caller($offset);
545 }
546
547 sub _make_unimport_sub {
548     shift;
549     my $exporting_package = shift;
550     my $exports           = shift;
551     my $export_recorder   = shift;
552     my $is_reexport       = shift;
553     my $meta_lookup       = shift;
554
555     return sub {
556         my $caller = scalar caller();
557         Moose::Exporter->_remove_keywords(
558             $caller,
559             [ keys %{$exports} ],
560             $export_recorder,
561             $is_reexport,
562         );
563     };
564 }
565
566 sub _remove_keywords {
567     shift;
568     my $package          = shift;
569     my $keywords         = shift;
570     my $recorded_exports = shift;
571     my $is_reexport      = shift;
572
573     no strict 'refs';
574
575     foreach my $name ( @{$keywords} ) {
576         if ( defined &{ $package . '::' . $name } ) {
577             my $sub = \&{ $package . '::' . $name };
578
579             # make sure it is from us
580             next unless $recorded_exports->{$sub};
581
582             if ( $is_reexport->{$name} ) {
583                 no strict 'refs';
584                 next
585                     unless _export_is_flagged(
586                             \*{ join q{::} => $package, $name } );
587             }
588
589             # and if it is from us, then undef the slot
590             delete ${ $package . '::' }{$name};
591         }
592     }
593 }
594
595 sub _make_init_meta {
596     shift;
597     my $class          = shift;
598     my $args           = shift;
599     my $meta_lookup    = shift;
600
601     my %old_style_roles;
602     for my $role (
603         map {"${_}_roles"}
604         qw(
605         metaclass
606         attribute_metaclass
607         method_metaclass
608         wrapped_method_metaclass
609         instance_metaclass
610         constructor_class
611         destructor_class
612         error_class
613         )
614         ) {
615         $old_style_roles{$role} = $args->{$role}
616             if exists $args->{$role};
617     }
618
619     my %base_class_roles;
620     %base_class_roles = ( roles => $args->{base_class_roles} )
621         if exists $args->{base_class_roles};
622
623     my %new_style_roles = map { $_ => $args->{$_} }
624         grep { exists $args->{$_} } qw( class_metaroles role_metaroles );
625
626     return unless %new_style_roles || %old_style_roles || %base_class_roles;
627
628     return sub {
629         shift;
630         my %options = @_;
631
632         return unless $meta_lookup->( $options{for_class} );
633
634         if ( %new_style_roles || %old_style_roles ) {
635             Moose::Util::MetaRole::apply_metaroles(
636                 for => $options{for_class},
637                 %new_style_roles,
638                 %old_style_roles,
639             );
640         }
641
642         Moose::Util::MetaRole::apply_base_class_roles(
643             for_class => $options{for_class},
644             %base_class_roles,
645             )
646             if $meta_lookup->( $options{for_class} )
647                 ->isa('Moose::Meta::Class');
648
649         return $meta_lookup->( $options{for_class} );
650     };
651 }
652
653 sub import {
654     strict->import;
655     warnings->import;
656 }
657
658 1;
659
660 # ABSTRACT: make an import() and unimport() just like Moose.pm
661
662 __END__
663
664 =head1 SYNOPSIS
665
666   package MyApp::Moose;
667
668   use Moose ();
669   use Moose::Exporter;
670
671   Moose::Exporter->setup_import_methods(
672       with_meta => [ 'has_rw', 'sugar2' ],
673       as_is     => [ 'sugar3', \&Some::Random::thing ],
674       also      => 'Moose',
675   );
676
677   sub has_rw {
678       my ( $meta, $name, %options ) = @_;
679       $meta->add_attribute(
680           $name,
681           is => 'rw',
682           %options,
683       );
684   }
685
686   # then later ...
687   package MyApp::User;
688
689   use MyApp::Moose;
690
691   has 'name';
692   has_rw 'size';
693   thing;
694
695   no MyApp::Moose;
696
697 =head1 DESCRIPTION
698
699 This module encapsulates the exporting of sugar functions in a
700 C<Moose.pm>-like manner. It does this by building custom C<import>,
701 C<unimport>, and C<init_meta> methods for your module, based on a spec you
702 provide.
703
704 It also lets you "stack" Moose-alike modules so you can export Moose's sugar
705 as well as your own, along with sugar from any random C<MooseX> module, as
706 long as they all use C<Moose::Exporter>. This feature exists to let you bundle
707 a set of MooseX modules into a policy module that developers can use directly
708 instead of using Moose itself.
709
710 To simplify writing exporter modules, C<Moose::Exporter> also imports
711 C<strict> and C<warnings> into your exporter module, as well as into
712 modules that use it.
713
714 =head1 METHODS
715
716 This module provides two public methods:
717
718 =over 4
719
720 =item  B<< Moose::Exporter->setup_import_methods(...) >>
721
722 When you call this method, C<Moose::Exporter> builds custom C<import>,
723 C<unimport>, and C<init_meta> methods for your module. The C<import> method
724 will export the functions you specify, and can also re-export functions
725 exported by some other module (like C<Moose.pm>).
726
727 The C<unimport> method cleans the caller's namespace of all the exported
728 functions. This includes any functions you re-export from other
729 packages. However, if the consumer of your package also imports those
730 functions from the original package, they will I<not> be cleaned.
731
732 If you pass any parameters for L<Moose::Util::MetaRole>, this method will
733 generate an C<init_meta> for you as well (see below for details). This
734 C<init_meta> will call C<Moose::Util::MetaRole::apply_metaroles> and
735 C<Moose::Util::MetaRole::apply_base_class_roles> as needed.
736
737 Note that if any of these methods already exist, they will not be
738 overridden, you will have to use C<build_import_methods> to get the
739 coderef that would be installed.
740
741 This method accepts the following parameters:
742
743 =over 8
744
745 =item * with_meta => [ ... ]
746
747 This list of function I<names only> will be wrapped and then exported. The
748 wrapper will pass the metaclass object for the caller as its first argument.
749
750 Many sugar functions will need to use this metaclass object to do something to
751 the calling package.
752
753 =item * as_is => [ ... ]
754
755 This list of function names or sub references will be exported as-is. You can
756 identify a subroutine by reference, which is handy to re-export some other
757 module's functions directly by reference (C<\&Some::Package::function>).
758
759 If you do export some other package's function, this function will never be
760 removed by the C<unimport> method. The reason for this is we cannot know if
761 the caller I<also> explicitly imported the sub themselves, and therefore wants
762 to keep it.
763
764 =item * trait_aliases => [ ... ]
765
766 This is a list of package names which should have shortened aliases exported,
767 similar to the functionality of L<aliased>. Each element in the list can be
768 either a package name, in which case the export will be named as the last
769 namespace component of the package, or an arrayref, whose first element is the
770 package to alias to, and second element is the alias to export.
771
772 =item * also => $name or \@names
773
774 This is a list of modules which contain functions that the caller
775 wants to export. These modules must also use C<Moose::Exporter>. The
776 most common use case will be to export the functions from C<Moose.pm>.
777 Functions specified by C<with_meta> or C<as_is> take precedence over
778 functions exported by modules specified by C<also>, so that a module
779 can selectively override functions exported by another module.
780
781 C<Moose::Exporter> also makes sure all these functions get removed
782 when C<unimport> is called.
783
784 =back
785
786 You can also provide parameters for C<Moose::Util::MetaRole::apply_metaroles>
787 and C<Moose::Util::MetaRole::base_class_roles>. Specifically, valid parameters
788 are "class_metaroles", "role_metaroles", and "base_class_roles".
789
790 =item B<< Moose::Exporter->build_import_methods(...) >>
791
792 Returns two or three code refs, one for C<import>, one for
793 C<unimport>, and optionally one for C<init_meta>, if the appropriate
794 options are passed in.
795
796 Accepts the additional C<install> option, which accepts an arrayref of method
797 names to install into your exporting package. The valid options are C<import>,
798 C<unimport>, and C<init_meta>. Calling C<setup_import_methods> is equivalent
799 to calling C<build_import_methods> with C<< install => [qw(import unimport
800 init_meta)] >> except that it doesn't also return the methods.
801
802 The C<import> method is built using L<Sub::Exporter>. This means that it can
803 take a hashref of the form C<< { into => $package } >> to specify the package
804 it operates on.
805
806 Used by C<setup_import_methods>.
807
808 =back
809
810 =head1 IMPORTING AND init_meta
811
812 If you want to set an alternative base object class or metaclass class, see
813 above for details on how this module can call L<Moose::Util::MetaRole> for
814 you.
815
816 If you want to do something that is not supported by this module, simply
817 define an C<init_meta> method in your class. The C<import> method that
818 C<Moose::Exporter> generates for you will call this method (if it exists). It
819 will always pass the caller to this method via the C<for_class> parameter.
820
821 Most of the time, your C<init_meta> method will probably just call C<<
822 Moose->init_meta >> to do the real work:
823
824   sub init_meta {
825       shift; # our class name
826       return Moose->init_meta( @_, metaclass => 'My::Metaclass' );
827   }
828
829 Keep in mind that C<build_import_methods> will return an C<init_meta>
830 method for you, which you can also call from within your custom
831 C<init_meta>:
832
833   my ( $import, $unimport, $init_meta )
834       = Moose::Exporter->build_import_methods(...);
835
836   sub import {
837       my $class = shift;
838
839       ...
840
841       # You can either pass an explicit package to import into ...
842       $class->$import( { into => scalar(caller) }, ... );
843
844       ...;
845   }
846
847   # ... or you can use 'goto' to provide the correct caller info to the
848   # generated method
849   sub unimport { goto &$unimport }
850
851   sub init_meta {
852       my $class = shift;
853
854       ...
855
856       $class->$init_meta(...);
857
858       ...
859   }
860
861 =head1 METACLASS TRAITS
862
863 The C<import> method generated by C<Moose::Exporter> will allow the
864 user of your module to specify metaclass traits in a C<-traits>
865 parameter passed as part of the import:
866
867   use Moose -traits => 'My::Meta::Trait';
868
869   use Moose -traits => [ 'My::Meta::Trait', 'My::Other::Trait' ];
870
871 These traits will be applied to the caller's metaclass
872 instance. Providing traits for an exporting class that does not create
873 a metaclass for the caller is an error.
874
875 =head1 BUGS
876
877 See L<Moose/BUGS> for details on reporting bugs.
878
879 =cut