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