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