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