Merged CMOP into Moose
[gitmo/Moose.git] / lib / Moose / Exporter.pm
CommitLineData
e606ae5f 1package Moose::Exporter;
2
3use strict;
4use warnings;
5
ae18d5ec 6our $AUTHORITY = 'cpan:STEVAN';
7
38bf2a25 8use XSLoader;
9
10BEGIN {
11 XSLoader::load(
12 'Moose',
13 $Moose::{VERSION} ? $Moose::{VERSION} : ()
14 );
15}
16
e606ae5f 17use Class::MOP;
18use List::MoreUtils qw( first_index uniq );
19use Moose::Util::MetaRole;
f88cfe7c 20use Scalar::Util qw(reftype);
091ac4b7 21use Sub::Exporter 0.980;
9f2230e9 22use Sub::Name qw(subname);
e606ae5f 23
24my %EXPORT_SPEC;
25
26sub setup_import_methods {
27 my ( $class, %args ) = @_;
28
29 my $exporting_package = $args{exporting_package} ||= caller();
30
95056a1e 31 $class->build_import_methods(
32 %args,
33 install => [qw(import unimport init_meta)]
34 );
e606ae5f 35}
36
37sub build_import_methods {
38 my ( $class, %args ) = @_;
39
40 my $exporting_package = $args{exporting_package} ||= caller();
41
42 $EXPORT_SPEC{$exporting_package} = \%args;
43
8fa582b1 44 my @exports_from = $class->_follow_also($exporting_package);
e606ae5f 45
46 my $export_recorder = {};
8fa582b1 47 my $is_reexport = {};
e606ae5f 48
e0d3eb10 49 my $exports = $class->_make_sub_exporter_params(
8fa582b1 50 [ @exports_from, $exporting_package ],
51 $export_recorder,
52 $is_reexport,
e0d3eb10 53 );
e606ae5f 54
d004fa18 55 my $exporter = $class->_make_exporter($exports, $is_reexport);
e606ae5f 56
95056a1e 57 my %methods;
8fa582b1 58 $methods{import} = $class->_make_import_sub(
59 $exporting_package,
60 $exporter,
61 \@exports_from,
62 $is_reexport
63 );
e606ae5f 64
8fa582b1 65 $methods{unimport} = $class->_make_unimport_sub(
66 $exporting_package,
67 $exports,
68 $export_recorder,
69 $is_reexport
70 );
95056a1e 71
8fa582b1 72 $methods{init_meta} = $class->_make_init_meta(
73 $exporting_package,
74 \%args
75 );
95056a1e 76
77 my $package = Class::MOP::Package->initialize($exporting_package);
906eabcd 78 for my $to_install ( @{ $args{install} || [] } ) {
95056a1e 79 my $symbol = '&' . $to_install;
906eabcd 80 next
81 unless $methods{$to_install}
82 && !$package->has_package_symbol($symbol);
83 $package->add_package_symbol( $symbol, $methods{$to_install} );
95056a1e 84 }
e606ae5f 85
8fa582b1 86 return ( $methods{import}, $methods{unimport}, $methods{init_meta} );
e606ae5f 87}
88
d004fa18 89sub _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
e606ae5f 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
8fa582b1 147 if ( !exists $EXPORT_SPEC{$exporting_package} ) {
ba1a3c2f 148 my $loaded = Class::MOP::is_class_loaded($exporting_package);
149
150 die "Package in also ($exporting_package) does not seem to "
8fa582b1 151 . "use Moose::Exporter"
152 . ( $loaded ? "" : " (is it loaded?)" );
ba1a3c2f 153 }
e606ae5f 154
155 my $also = $EXPORT_SPEC{$exporting_package}{also};
156
157 return unless defined $also;
158
159 my @also = ref $also ? @{$also} : $also;
160
8fa582b1 161 for my $package (@also) {
162 die
163 "Circular reference in 'also' parameter to Moose::Exporter between $exporting_package and $package"
e606ae5f 164 if $seen->{$package};
165
166 $seen->{$package} = 1;
167 }
168
169 return @also, map { _follow_also_real($_) } @also;
170 }
171}
172
f88cfe7c 173sub _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
e606ae5f 197sub _make_sub_exporter_params {
8fa582b1 198 my $class = shift;
199 my $packages = shift;
200 my $export_recorder = shift;
201 my $is_reexport = shift;
e606ae5f 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
5ac14e89 209 for my $name ( @{ $args->{with_meta} } ) {
0dd4228e 210 my $sub = $class->_sub_from_package( $package, $name )
211 or next;
e6a5040f 212
e606ae5f 213 my $fq_name = $package . '::' . $name;
214
5ac14e89 215 $exports{$name} = $class->_make_wrapped_sub_with_meta(
e606ae5f 216 $fq_name,
217 $sub,
218 $export_recorder,
219 );
220 }
221
5ac14e89 222 for my $name ( @{ $args->{with_caller} } ) {
0dd4228e 223 my $sub = $class->_sub_from_package( $package, $name )
224 or next;
e6a5040f 225
45975bce 226 my $fq_name = $package . '::' . $name;
227
5ac14e89 228 $exports{$name} = $class->_make_wrapped_sub(
45975bce 229 $fq_name,
230 $sub,
231 $export_recorder,
232 );
45975bce 233 }
234
f88cfe7c 235 my @extra_exports = $class->_parse_trait_aliases(
236 $package, $args->{trait_aliases},
237 );
238 for my $name ( @{ $args->{as_is} }, @extra_exports ) {
8fa582b1 239 my ( $sub, $coderef_name );
e606ae5f 240
241 if ( ref $name ) {
e0d3eb10 242 $sub = $name;
e05fb8ae 243
e05fb8ae 244 my $coderef_pkg;
e6a5040f 245 ( $coderef_pkg, $coderef_name )
246 = Class::MOP::get_code_info($name);
e05fb8ae 247
e0d3eb10 248 if ( $coderef_pkg ne $package ) {
8fa582b1 249 $is_reexport->{$coderef_name} = 1;
e0d3eb10 250 }
e606ae5f 251 }
252 else {
0dd4228e 253 $sub = $class->_sub_from_package( $package, $name )
254 or next;
e6a5040f 255
e6a5040f 256 $coderef_name = $name;
e606ae5f 257 }
258
259 $export_recorder->{$sub} = 1;
260
8fa582b1 261 $exports{$coderef_name} = sub {$sub};
e606ae5f 262 }
263 }
264
e0d3eb10 265 return \%exports;
e606ae5f 266}
267
0dd4228e 268sub _sub_from_package {
8fa582b1 269 my $sclass = shift;
0dd4228e 270 my $package = shift;
8fa582b1 271 my $name = shift;
0dd4228e 272
273 my $sub = do {
274 no strict 'refs';
275 \&{ $package . '::' . $name };
276 };
277
278 return $sub if defined &$sub;
279
8fa582b1 280 Carp::cluck "Trying to export undefined sub ${package}::${name}";
0dd4228e 281
282 return;
283}
284
96bb13ea 285our $CALLER;
286
287sub _make_wrapped_sub {
b4f00a34 288 my $self = shift;
96bb13ea 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
8fa582b1 302 my $wrapper = $self->_curry_wrapper( $sub, $fq_name, $caller );
b4f00a34 303
8fa582b1 304 my $sub = subname( $fq_name => $wrapper );
96bb13ea 305
306 $export_recorder->{$sub} = 1;
307
308 return $sub;
309 };
310}
e606ae5f 311
45975bce 312sub _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
8fa582b1 321 my $wrapper = $self->_late_curry_wrapper(
322 $sub, $fq_name,
323 sub { Class::MOP::class_of(shift) } => $caller
324 );
45975bce 325
8fa582b1 326 my $sub = subname( $fq_name => $wrapper );
45975bce 327
328 $export_recorder->{$sub} = 1;
329
330 return $sub;
331 };
332}
333
6de00734 334sub _curry_wrapper {
badbc528 335 my $class = shift;
b4f00a34 336 my $sub = shift;
337 my $fq_name = shift;
6de00734 338 my @extra = @_;
b4f00a34 339
8fa582b1 340 my $wrapper = sub { $sub->( @extra, @_ ) };
341 if ( my $proto = prototype $sub ) {
342
2d7e979b 343 # XXX - Perl's prototype sucks. Use & to make set_prototype
6de00734 344 # ignore the fact that we're passing "private variables"
8fa582b1 345 &Scalar::Util::set_prototype( $wrapper, $proto );
badbc528 346 }
347 return $wrapper;
b4f00a34 348}
349
45975bce 350sub _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 {
8fa582b1 358
45975bce 359 # resolve curried arguments at runtime via this closure
8fa582b1 360 my @curry = ( $extra->(@ex_args) );
361 return $sub->( @curry, @_ );
45975bce 362 };
363
8fa582b1 364 if ( my $proto = prototype $sub ) {
365
45975bce 366 # XXX - Perl's prototype sucks. Use & to make set_prototype
367 # ignore the fact that we're passing "private variables"
8fa582b1 368 &Scalar::Util::set_prototype( $wrapper, $proto );
45975bce 369 }
370 return $wrapper;
371}
372
96bb13ea 373sub _make_import_sub {
374 shift;
375 my $exporting_package = shift;
376 my $exporter = shift;
377 my $exports_from = shift;
8fa582b1 378 my $is_reexport = shift;
96bb13ea 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
8f30b86e 393 my $metaclass;
394 ( $metaclass, @_ ) = _strip_metaclass(@_);
8fa582b1 395 $metaclass
396 = Moose::Util::resolve_metaclass_alias( 'Class' => $metaclass )
397 if defined $metaclass && length $metaclass;
8f30b86e 398
2937ed18 399 my $meta_name;
400 ( $meta_name, @_ ) = _strip_meta_name(@_);
d65bfd76 401
96bb13ea 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
96bb13ea 418 my $did_init_meta;
419 for my $c ( grep { $_->can('init_meta') } $class, @{$exports_from} ) {
8fa582b1 420
816208bc 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.
fdeb8354 424 local $CALLER = $CALLER;
d65bfd76 425 $c->init_meta(
426 for_class => $CALLER,
427 metaclass => $metaclass,
2937ed18 428 meta_name => $meta_name,
d65bfd76 429 );
96bb13ea 430 $did_init_meta = 1;
431 }
e606ae5f 432
96bb13ea 433 if ( $did_init_meta && @{$traits} ) {
8fa582b1 434
96bb13ea 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} ) {
70ea9161 442 require Moose;
96bb13ea 443 Moose->throw_error(
444 "Cannot provide traits when $class does not have an init_meta() method"
445 );
446 }
e606ae5f 447
8fa582b1 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 );
96bb13ea 458 };
e606ae5f 459}
460
461sub _strip_traits {
1c3304e6 462 my $idx = first_index { ( $_ || '' ) eq '-traits' } @_;
e606ae5f 463
464 return ( [], @_ ) unless $idx >= 0 && $#_ >= $idx + 1;
465
466 my $traits = $_[ $idx + 1 ];
467
468 splice @_, $idx, 2;
469
8fa582b1 470 $traits = [$traits] unless ref $traits;
e606ae5f 471
472 return ( $traits, @_ );
473}
474
8f30b86e 475sub _strip_metaclass {
1c3304e6 476 my $idx = first_index { ( $_ || '' ) eq '-metaclass' } @_;
8f30b86e 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
2937ed18 487sub _strip_meta_name {
1c3304e6 488 my $idx = first_index { ( $_ || '' ) eq '-meta_name' } @_;
d65bfd76 489
2937ed18 490 return ( 'meta', @_ ) unless $idx >= 0 && $#_ >= $idx + 1;
d65bfd76 491
2937ed18 492 my $meta_name = $_[ $idx + 1 ];
d65bfd76 493
494 splice @_, $idx, 2;
495
2937ed18 496 return ( $meta_name, @_ );
d65bfd76 497}
498
e606ae5f 499sub _apply_meta_traits {
500 my ( $class, $traits ) = @_;
501
502 return unless @{$traits};
503
2571a16d 504 my $meta = Class::MOP::class_of($class);
e606ae5f 505
506 my $type = ( split /::/, ref $meta )[-1]
c245d69b 507 or Moose->throw_error(
e606ae5f 508 'Cannot determine metaclass type for trait application . Meta isa '
8fa582b1 509 . ref $meta );
e606ae5f 510
8fa582b1 511 my @resolved_traits = map {
512 ref $_
513 ? $_
514 : Moose::Util::resolve_metatrait_alias( $type => $_ )
515 } @$traits;
e606ae5f 516
517 return unless @resolved_traits;
518
f785aad8 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);
e606ae5f 529}
530
531sub _get_caller {
8fa582b1 532
e606ae5f 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
544sub _make_unimport_sub {
545 shift;
546 my $exporting_package = shift;
547 my $exports = shift;
548 my $export_recorder = shift;
8fa582b1 549 my $is_reexport = shift;
e606ae5f 550
551 return sub {
552 my $caller = scalar caller();
553 Moose::Exporter->_remove_keywords(
554 $caller,
555 [ keys %{$exports} ],
556 $export_recorder,
8fa582b1 557 $is_reexport,
e606ae5f 558 );
559 };
560}
561
562sub _remove_keywords {
563 shift;
564 my $package = shift;
565 my $keywords = shift;
566 my $recorded_exports = shift;
8fa582b1 567 my $is_reexport = shift;
e606ae5f 568
569 no strict 'refs';
570
8fa582b1 571 foreach my $name ( @{$keywords} ) {
e606ae5f 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
8fa582b1 578 if ( $is_reexport->{$name} ) {
579 no strict 'refs';
580 next
581 unless _export_is_flagged(
582 \*{ join q{::} => $package, $name } );
583 }
584
e606ae5f 585 # and if it is from us, then undef the slot
586 delete ${ $package . '::' }{$name};
587 }
588 }
589}
590
95056a1e 591sub _make_init_meta {
592 shift;
906eabcd 593 my $class = shift;
594 my $args = shift;
95056a1e 595
f785aad8 596 my %old_style_roles;
906eabcd 597 for my $role (
598 map {"${_}_roles"}
f785aad8 599 qw(
600 metaclass
906eabcd 601 attribute_metaclass
602 method_metaclass
603 wrapped_method_metaclass
604 instance_metaclass
605 constructor_class
606 destructor_class
607 error_class
f785aad8 608 )
906eabcd 609 ) {
f785aad8 610 $old_style_roles{$role} = $args->{$role}
611 if exists $args->{$role};
95056a1e 612 }
613
614 my %base_class_roles;
906eabcd 615 %base_class_roles = ( roles => $args->{base_class_roles} )
95056a1e 616 if exists $args->{base_class_roles};
617
f785aad8 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;
95056a1e 622
623 return sub {
624 shift;
625 my %options = @_;
906eabcd 626
627 return unless Class::MOP::class_of( $options{for_class} );
628
3b400403 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 }
906eabcd 636
95056a1e 637 Moose::Util::MetaRole::apply_base_class_roles(
638 for_class => $options{for_class},
639 %base_class_roles,
906eabcd 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} );
95056a1e 645 };
646}
647
e2fa092d 648sub import {
649 strict->import;
650 warnings->import;
651}
652
e606ae5f 6531;
654
ad46f524 655# ABSTRACT: make an import() and unimport() just like Moose.pm
e606ae5f 656
ad46f524 657__END__
e606ae5f 658
659=head1 SYNOPSIS
660
661 package MyApp::Moose;
662
e606ae5f 663 use Moose ();
664 use Moose::Exporter;
665
666 Moose::Exporter->setup_import_methods(
5ac14e89 667 with_meta => [ 'has_rw', 'sugar2' ],
668 as_is => [ 'sugar3', \&Some::Random::thing ],
669 also => 'Moose',
e606ae5f 670 );
671
82ad7804 672 sub has_rw {
5ac14e89 673 my ( $meta, $name, %options ) = @_;
674 $meta->add_attribute(
675 $name,
82ad7804 676 is => 'rw',
677 %options,
678 );
679 }
680
e606ae5f 681 # then later ...
682 package MyApp::User;
683
684 use MyApp::Moose;
685
686 has 'name';
6daad0b9 687 has_rw 'size';
e606ae5f 688 thing;
689
690 no MyApp::Moose;
691
692=head1 DESCRIPTION
693
fd7ab111 694This module encapsulates the exporting of sugar functions in a
95056a1e 695C<Moose.pm>-like manner. It does this by building custom C<import>,
37e4fe95 696C<unimport>, and C<init_meta> methods for your module, based on a spec you
697provide.
e606ae5f 698
37e4fe95 699It also lets you "stack" Moose-alike modules so you can export Moose's sugar
700as well as your own, along with sugar from any random C<MooseX> module, as
701long as they all use C<Moose::Exporter>. This feature exists to let you bundle
702a set of MooseX modules into a policy module that developers can use directly
703instead of using Moose itself.
e606ae5f 704
10e0127a 705To simplify writing exporter modules, C<Moose::Exporter> also imports
706C<strict> and C<warnings> into your exporter module, as well as into
707modules that use it.
708
e606ae5f 709=head1 METHODS
710
711This module provides two public methods:
712
4b68e0de 713=over 4
714
715=item B<< Moose::Exporter->setup_import_methods(...) >>
e606ae5f 716
95056a1e 717When you call this method, C<Moose::Exporter> builds custom C<import>,
37e4fe95 718C<unimport>, and C<init_meta> methods for your module. The C<import> method
719will export the functions you specify, and can also re-export functions
720exported by some other module (like C<Moose.pm>).
e606ae5f 721
37e4fe95 722The C<unimport> method cleans the caller's namespace of all the exported
cee38bb4 723functions. This includes any functions you re-export from other
724packages. However, if the consumer of your package also imports those
725functions from the original package, they will I<not> be cleaned.
e606ae5f 726
37e4fe95 727If you pass any parameters for L<Moose::Util::MetaRole>, this method will
728generate an C<init_meta> for you as well (see below for details). This
ff526a65 729C<init_meta> will call C<Moose::Util::MetaRole::apply_metaroles> and
37e4fe95 730C<Moose::Util::MetaRole::apply_base_class_roles> as needed.
95056a1e 731
732Note that if any of these methods already exist, they will not be
733overridden, you will have to use C<build_import_methods> to get the
734coderef that would be installed.
e606ae5f 735
736This method accepts the following parameters:
737
4b68e0de 738=over 8
e606ae5f 739
5ac14e89 740=item * with_meta => [ ... ]
e606ae5f 741
37e4fe95 742This list of function I<names only> will be wrapped and then exported. The
5ac14e89 743wrapper will pass the metaclass object for the caller as its first argument.
744
745Many sugar functions will need to use this metaclass object to do something to
746the calling package.
e606ae5f 747
748=item * as_is => [ ... ]
749
37e4fe95 750This list of function names or sub references will be exported as-is. You can
751identify a subroutine by reference, which is handy to re-export some other
752module's functions directly by reference (C<\&Some::Package::function>).
e606ae5f 753
37e4fe95 754If you do export some other package's function, this function will never be
755removed by the C<unimport> method. The reason for this is we cannot know if
756the caller I<also> explicitly imported the sub themselves, and therefore wants
757to keep it.
e05fb8ae 758
f88cfe7c 759=item * trait_aliases => [ ... ]
760
bcbb1f7b 761This is a list of package names which should have shortened aliases exported,
f88cfe7c 762similar to the functionality of L<aliased>. Each element in the list can be
763either a package name, in which case the export will be named as the last
764namespace component of the package, or an arrayref, whose first element is the
765package to alias to, and second element is the alias to export.
766
e606ae5f 767=item * also => $name or \@names
768
769This is a list of modules which contain functions that the caller
770wants to export. These modules must also use C<Moose::Exporter>. The
771most common use case will be to export the functions from C<Moose.pm>.
5ac14e89 772Functions specified by C<with_meta> or C<as_is> take precedence over
ae8817b6 773functions exported by modules specified by C<also>, so that a module
774can selectively override functions exported by another module.
e606ae5f 775
776C<Moose::Exporter> also makes sure all these functions get removed
777when C<unimport> is called.
778
779=back
780
f785aad8 781You can also provide parameters for C<Moose::Util::MetaRole::apply_metaroles>
782and C<Moose::Util::MetaRole::base_class_roles>. Specifically, valid parameters
15851b87 783are "class_metaroles", "role_metaroles", and "base_class_roles".
95056a1e 784
4b68e0de 785=item B<< Moose::Exporter->build_import_methods(...) >>
e606ae5f 786
95056a1e 787Returns two or three code refs, one for C<import>, one for
788C<unimport>, and optionally one for C<init_meta>, if the appropriate
789options are passed in.
790
37e4fe95 791Accepts the additional C<install> option, which accepts an arrayref of method
792names to install into your exporting package. The valid options are C<import>,
793C<unimport>, and C<init_meta>. Calling C<setup_import_methods> is equivalent
794to calling C<build_import_methods> with C<< install => [qw(import unimport
795init_meta)] >> except that it doesn't also return the methods.
e606ae5f 796
797Used by C<setup_import_methods>.
798
4b68e0de 799=back
800
e606ae5f 801=head1 IMPORTING AND init_meta
802
37e4fe95 803If you want to set an alternative base object class or metaclass class, see
804above for details on how this module can call L<Moose::Util::MetaRole> for
805you.
806
807If you want to do something that is not supported by this module, simply
808define an C<init_meta> method in your class. The C<import> method that
809C<Moose::Exporter> generates for you will call this method (if it exists). It
810will always pass the caller to this method via the C<for_class> parameter.
e606ae5f 811
812Most of the time, your C<init_meta> method will probably just call C<<
813Moose->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
95056a1e 820Keep in mind that C<build_import_methods> will return an C<init_meta>
821method for you, which you can also call from within your custom
37e4fe95 822C<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 }
95056a1e 848
e606ae5f 849=head1 METACLASS TRAITS
850
851The C<import> method generated by C<Moose::Exporter> will allow the
852user of your module to specify metaclass traits in a C<-traits>
853parameter 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
859These traits will be applied to the caller's metaclass
860instance. Providing traits for an exporting class that does not create
861a metaclass for the caller is an error.
862
c5fc2c21 863=head1 BUGS
864
865See L<Moose/BUGS> for details on reporting bugs.
866
e606ae5f 867=cut