be a bit stricter and more consistent with tc messages
[gitmo/Moose.git] / lib / Moose / Exporter.pm
CommitLineData
e606ae5f 1package Moose::Exporter;
2
3use strict;
4use warnings;
5
e606ae5f 6use Class::MOP;
7use List::MoreUtils qw( first_index uniq );
8use Moose::Util::MetaRole;
f88cfe7c 9use Scalar::Util qw(reftype);
091ac4b7 10use Sub::Exporter 0.980;
9f2230e9 11use Sub::Name qw(subname);
e606ae5f 12
13my %EXPORT_SPEC;
14
15sub setup_import_methods {
16 my ( $class, %args ) = @_;
17
2a591aa7 18 $args{exporting_package} ||= caller();
e606ae5f 19
95056a1e 20 $class->build_import_methods(
21 %args,
22 install => [qw(import unimport init_meta)]
23 );
e606ae5f 24}
25
26sub build_import_methods {
27 my ( $class, %args ) = @_;
28
29 my $exporting_package = $args{exporting_package} ||= caller();
30
31 $EXPORT_SPEC{$exporting_package} = \%args;
32
8fa582b1 33 my @exports_from = $class->_follow_also($exporting_package);
e606ae5f 34
35 my $export_recorder = {};
8fa582b1 36 my $is_reexport = {};
e606ae5f 37
e0d3eb10 38 my $exports = $class->_make_sub_exporter_params(
8fa582b1 39 [ @exports_from, $exporting_package ],
40 $export_recorder,
41 $is_reexport,
e0d3eb10 42 );
e606ae5f 43
d004fa18 44 my $exporter = $class->_make_exporter($exports, $is_reexport);
e606ae5f 45
95056a1e 46 my %methods;
8fa582b1 47 $methods{import} = $class->_make_import_sub(
48 $exporting_package,
49 $exporter,
50 \@exports_from,
51 $is_reexport
52 );
e606ae5f 53
8fa582b1 54 $methods{unimport} = $class->_make_unimport_sub(
55 $exporting_package,
56 $exports,
57 $export_recorder,
58 $is_reexport
59 );
95056a1e 60
8fa582b1 61 $methods{init_meta} = $class->_make_init_meta(
62 $exporting_package,
63 \%args
64 );
95056a1e 65
66 my $package = Class::MOP::Package->initialize($exporting_package);
906eabcd 67 for my $to_install ( @{ $args{install} || [] } ) {
95056a1e 68 my $symbol = '&' . $to_install;
906eabcd 69 next
70 unless $methods{$to_install}
71 && !$package->has_package_symbol($symbol);
72 $package->add_package_symbol( $symbol, $methods{$to_install} );
95056a1e 73 }
e606ae5f 74
8fa582b1 75 return ( $methods{import}, $methods{unimport}, $methods{init_meta} );
e606ae5f 76}
77
d004fa18 78sub _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
e606ae5f 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
f7d62c2e 130 return reverse uniq( _follow_also_real($exporting_package) );
e606ae5f 131 }
132
133 sub _follow_also_real {
134 my $exporting_package = shift;
135
8fa582b1 136 if ( !exists $EXPORT_SPEC{$exporting_package} ) {
ba1a3c2f 137 my $loaded = Class::MOP::is_class_loaded($exporting_package);
138
139 die "Package in also ($exporting_package) does not seem to "
8fa582b1 140 . "use Moose::Exporter"
141 . ( $loaded ? "" : " (is it loaded?)" );
ba1a3c2f 142 }
e606ae5f 143
144 my $also = $EXPORT_SPEC{$exporting_package}{also};
145
146 return unless defined $also;
147
148 my @also = ref $also ? @{$also} : $also;
149
8fa582b1 150 for my $package (@also) {
151 die
152 "Circular reference in 'also' parameter to Moose::Exporter between $exporting_package and $package"
e606ae5f 153 if $seen->{$package};
154
155 $seen->{$package} = 1;
156 }
157
158 return @also, map { _follow_also_real($_) } @also;
159 }
160}
161
f88cfe7c 162sub _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
e606ae5f 186sub _make_sub_exporter_params {
8fa582b1 187 my $class = shift;
188 my $packages = shift;
189 my $export_recorder = shift;
ce5ccc1a 190 my $is_reexport = shift;
e606ae5f 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
5ac14e89 198 for my $name ( @{ $args->{with_meta} } ) {
0dd4228e 199 my $sub = $class->_sub_from_package( $package, $name )
200 or next;
e6a5040f 201
e606ae5f 202 my $fq_name = $package . '::' . $name;
203
5ac14e89 204 $exports{$name} = $class->_make_wrapped_sub_with_meta(
e606ae5f 205 $fq_name,
206 $sub,
207 $export_recorder,
208 );
209 }
210
5ac14e89 211 for my $name ( @{ $args->{with_caller} } ) {
0dd4228e 212 my $sub = $class->_sub_from_package( $package, $name )
213 or next;
e6a5040f 214
45975bce 215 my $fq_name = $package . '::' . $name;
216
5ac14e89 217 $exports{$name} = $class->_make_wrapped_sub(
45975bce 218 $fq_name,
219 $sub,
220 $export_recorder,
221 );
45975bce 222 }
223
f88cfe7c 224 my @extra_exports = $class->_parse_trait_aliases(
225 $package, $args->{trait_aliases},
226 );
227 for my $name ( @{ $args->{as_is} }, @extra_exports ) {
8fa582b1 228 my ( $sub, $coderef_name );
e606ae5f 229
230 if ( ref $name ) {
e0d3eb10 231 $sub = $name;
e05fb8ae 232
e05fb8ae 233 my $coderef_pkg;
e6a5040f 234 ( $coderef_pkg, $coderef_name )
235 = Class::MOP::get_code_info($name);
e05fb8ae 236
e0d3eb10 237 if ( $coderef_pkg ne $package ) {
8fa582b1 238 $is_reexport->{$coderef_name} = 1;
e0d3eb10 239 }
e606ae5f 240 }
241 else {
0dd4228e 242 $sub = $class->_sub_from_package( $package, $name )
243 or next;
e6a5040f 244
e6a5040f 245 $coderef_name = $name;
e606ae5f 246 }
247
248 $export_recorder->{$sub} = 1;
249
8fa582b1 250 $exports{$coderef_name} = sub {$sub};
e606ae5f 251 }
252 }
253
e0d3eb10 254 return \%exports;
e606ae5f 255}
256
0dd4228e 257sub _sub_from_package {
8fa582b1 258 my $sclass = shift;
0dd4228e 259 my $package = shift;
8fa582b1 260 my $name = shift;
0dd4228e 261
262 my $sub = do {
263 no strict 'refs';
264 \&{ $package . '::' . $name };
265 };
266
267 return $sub if defined &$sub;
268
8fa582b1 269 Carp::cluck "Trying to export undefined sub ${package}::${name}";
0dd4228e 270
271 return;
272}
273
96bb13ea 274our $CALLER;
275
276sub _make_wrapped_sub {
b4f00a34 277 my $self = shift;
96bb13ea 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
8fa582b1 291 my $wrapper = $self->_curry_wrapper( $sub, $fq_name, $caller );
b4f00a34 292
8fa582b1 293 my $sub = subname( $fq_name => $wrapper );
96bb13ea 294
295 $export_recorder->{$sub} = 1;
296
297 return $sub;
298 };
299}
e606ae5f 300
45975bce 301sub _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
8fa582b1 310 my $wrapper = $self->_late_curry_wrapper(
311 $sub, $fq_name,
312 sub { Class::MOP::class_of(shift) } => $caller
313 );
45975bce 314
8fa582b1 315 my $sub = subname( $fq_name => $wrapper );
45975bce 316
317 $export_recorder->{$sub} = 1;
318
319 return $sub;
320 };
321}
322
6de00734 323sub _curry_wrapper {
badbc528 324 my $class = shift;
b4f00a34 325 my $sub = shift;
326 my $fq_name = shift;
6de00734 327 my @extra = @_;
b4f00a34 328
8fa582b1 329 my $wrapper = sub { $sub->( @extra, @_ ) };
330 if ( my $proto = prototype $sub ) {
331
2d7e979b 332 # XXX - Perl's prototype sucks. Use & to make set_prototype
6de00734 333 # ignore the fact that we're passing "private variables"
8fa582b1 334 &Scalar::Util::set_prototype( $wrapper, $proto );
badbc528 335 }
336 return $wrapper;
b4f00a34 337}
338
45975bce 339sub _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 {
8fa582b1 347
45975bce 348 # resolve curried arguments at runtime via this closure
8fa582b1 349 my @curry = ( $extra->(@ex_args) );
350 return $sub->( @curry, @_ );
45975bce 351 };
352
8fa582b1 353 if ( my $proto = prototype $sub ) {
354
45975bce 355 # XXX - Perl's prototype sucks. Use & to make set_prototype
356 # ignore the fact that we're passing "private variables"
8fa582b1 357 &Scalar::Util::set_prototype( $wrapper, $proto );
45975bce 358 }
359 return $wrapper;
360}
361
96bb13ea 362sub _make_import_sub {
363 shift;
364 my $exporting_package = shift;
365 my $exporter = shift;
366 my $exports_from = shift;
ce5ccc1a 367 my $is_reexport = shift;
96bb13ea 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
8f30b86e 382 my $metaclass;
383 ( $metaclass, @_ ) = _strip_metaclass(@_);
8fa582b1 384 $metaclass
385 = Moose::Util::resolve_metaclass_alias( 'Class' => $metaclass )
386 if defined $metaclass && length $metaclass;
8f30b86e 387
2937ed18 388 my $meta_name;
389 ( $meta_name, @_ ) = _strip_meta_name(@_);
d65bfd76 390
96bb13ea 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
96bb13ea 407 my $did_init_meta;
408 for my $c ( grep { $_->can('init_meta') } $class, @{$exports_from} ) {
8fa582b1 409
816208bc 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.
fdeb8354 413 local $CALLER = $CALLER;
d65bfd76 414 $c->init_meta(
415 for_class => $CALLER,
416 metaclass => $metaclass,
2937ed18 417 meta_name => $meta_name,
d65bfd76 418 );
96bb13ea 419 $did_init_meta = 1;
420 }
e606ae5f 421
96bb13ea 422 if ( $did_init_meta && @{$traits} ) {
8fa582b1 423
96bb13ea 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} ) {
70ea9161 431 require Moose;
96bb13ea 432 Moose->throw_error(
433 "Cannot provide traits when $class does not have an init_meta() method"
434 );
435 }
e606ae5f 436
8fa582b1 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 );
96bb13ea 447 };
e606ae5f 448}
449
450sub _strip_traits {
1c3304e6 451 my $idx = first_index { ( $_ || '' ) eq '-traits' } @_;
e606ae5f 452
453 return ( [], @_ ) unless $idx >= 0 && $#_ >= $idx + 1;
454
455 my $traits = $_[ $idx + 1 ];
456
457 splice @_, $idx, 2;
458
8fa582b1 459 $traits = [$traits] unless ref $traits;
e606ae5f 460
461 return ( $traits, @_ );
462}
463
8f30b86e 464sub _strip_metaclass {
1c3304e6 465 my $idx = first_index { ( $_ || '' ) eq '-metaclass' } @_;
8f30b86e 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
2937ed18 476sub _strip_meta_name {
1c3304e6 477 my $idx = first_index { ( $_ || '' ) eq '-meta_name' } @_;
d65bfd76 478
2937ed18 479 return ( 'meta', @_ ) unless $idx >= 0 && $#_ >= $idx + 1;
d65bfd76 480
2937ed18 481 my $meta_name = $_[ $idx + 1 ];
d65bfd76 482
483 splice @_, $idx, 2;
484
2937ed18 485 return ( $meta_name, @_ );
d65bfd76 486}
487
e606ae5f 488sub _apply_meta_traits {
489 my ( $class, $traits ) = @_;
490
491 return unless @{$traits};
492
2571a16d 493 my $meta = Class::MOP::class_of($class);
e606ae5f 494
495 my $type = ( split /::/, ref $meta )[-1]
c245d69b 496 or Moose->throw_error(
e606ae5f 497 'Cannot determine metaclass type for trait application . Meta isa '
8fa582b1 498 . ref $meta );
e606ae5f 499
8fa582b1 500 my @resolved_traits = map {
501 ref $_
502 ? $_
503 : Moose::Util::resolve_metatrait_alias( $type => $_ )
504 } @$traits;
e606ae5f 505
506 return unless @resolved_traits;
507
f785aad8 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);
e606ae5f 518}
519
520sub _get_caller {
8fa582b1 521
e606ae5f 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
533sub _make_unimport_sub {
534 shift;
535 my $exporting_package = shift;
536 my $exports = shift;
537 my $export_recorder = shift;
ce5ccc1a 538 my $is_reexport = shift;
e606ae5f 539
540 return sub {
541 my $caller = scalar caller();
542 Moose::Exporter->_remove_keywords(
543 $caller,
544 [ keys %{$exports} ],
545 $export_recorder,
8fa582b1 546 $is_reexport,
e606ae5f 547 );
548 };
549}
550
551sub _remove_keywords {
552 shift;
553 my $package = shift;
554 my $keywords = shift;
555 my $recorded_exports = shift;
ce5ccc1a 556 my $is_reexport = shift;
e606ae5f 557
558 no strict 'refs';
559
8fa582b1 560 foreach my $name ( @{$keywords} ) {
e606ae5f 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
8fa582b1 567 if ( $is_reexport->{$name} ) {
568 no strict 'refs';
569 next
570 unless _export_is_flagged(
571 \*{ join q{::} => $package, $name } );
572 }
573
e606ae5f 574 # and if it is from us, then undef the slot
575 delete ${ $package . '::' }{$name};
576 }
577 }
578}
579
95056a1e 580sub _make_init_meta {
581 shift;
906eabcd 582 my $class = shift;
583 my $args = shift;
95056a1e 584
f785aad8 585 my %old_style_roles;
906eabcd 586 for my $role (
587 map {"${_}_roles"}
f785aad8 588 qw(
589 metaclass
906eabcd 590 attribute_metaclass
591 method_metaclass
592 wrapped_method_metaclass
593 instance_metaclass
594 constructor_class
595 destructor_class
596 error_class
f785aad8 597 )
906eabcd 598 ) {
f785aad8 599 $old_style_roles{$role} = $args->{$role}
600 if exists $args->{$role};
95056a1e 601 }
602
603 my %base_class_roles;
906eabcd 604 %base_class_roles = ( roles => $args->{base_class_roles} )
95056a1e 605 if exists $args->{base_class_roles};
606
f785aad8 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;
95056a1e 611
612 return sub {
613 shift;
614 my %options = @_;
906eabcd 615
616 return unless Class::MOP::class_of( $options{for_class} );
617
3b400403 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 }
906eabcd 625
95056a1e 626 Moose::Util::MetaRole::apply_base_class_roles(
627 for_class => $options{for_class},
628 %base_class_roles,
906eabcd 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} );
95056a1e 634 };
635}
636
e2fa092d 637sub import {
638 strict->import;
639 warnings->import;
640}
641
e606ae5f 6421;
643
ad46f524 644# ABSTRACT: make an import() and unimport() just like Moose.pm
e606ae5f 645
ad46f524 646__END__
e606ae5f 647
648=head1 SYNOPSIS
649
650 package MyApp::Moose;
651
e606ae5f 652 use Moose ();
653 use Moose::Exporter;
654
655 Moose::Exporter->setup_import_methods(
5ac14e89 656 with_meta => [ 'has_rw', 'sugar2' ],
657 as_is => [ 'sugar3', \&Some::Random::thing ],
658 also => 'Moose',
e606ae5f 659 );
660
82ad7804 661 sub has_rw {
5ac14e89 662 my ( $meta, $name, %options ) = @_;
663 $meta->add_attribute(
664 $name,
82ad7804 665 is => 'rw',
666 %options,
667 );
668 }
669
e606ae5f 670 # then later ...
671 package MyApp::User;
672
673 use MyApp::Moose;
674
675 has 'name';
6daad0b9 676 has_rw 'size';
e606ae5f 677 thing;
678
679 no MyApp::Moose;
680
681=head1 DESCRIPTION
682
fd7ab111 683This module encapsulates the exporting of sugar functions in a
95056a1e 684C<Moose.pm>-like manner. It does this by building custom C<import>,
37e4fe95 685C<unimport>, and C<init_meta> methods for your module, based on a spec you
686provide.
e606ae5f 687
37e4fe95 688It also lets you "stack" Moose-alike modules so you can export Moose's sugar
689as well as your own, along with sugar from any random C<MooseX> module, as
690long as they all use C<Moose::Exporter>. This feature exists to let you bundle
691a set of MooseX modules into a policy module that developers can use directly
692instead of using Moose itself.
e606ae5f 693
10e0127a 694To simplify writing exporter modules, C<Moose::Exporter> also imports
695C<strict> and C<warnings> into your exporter module, as well as into
696modules that use it.
697
e606ae5f 698=head1 METHODS
699
700This module provides two public methods:
701
4b68e0de 702=over 4
703
704=item B<< Moose::Exporter->setup_import_methods(...) >>
e606ae5f 705
95056a1e 706When you call this method, C<Moose::Exporter> builds custom C<import>,
37e4fe95 707C<unimport>, and C<init_meta> methods for your module. The C<import> method
708will export the functions you specify, and can also re-export functions
709exported by some other module (like C<Moose.pm>).
e606ae5f 710
37e4fe95 711The C<unimport> method cleans the caller's namespace of all the exported
cee38bb4 712functions. This includes any functions you re-export from other
713packages. However, if the consumer of your package also imports those
714functions from the original package, they will I<not> be cleaned.
e606ae5f 715
37e4fe95 716If you pass any parameters for L<Moose::Util::MetaRole>, this method will
717generate an C<init_meta> for you as well (see below for details). This
ff526a65 718C<init_meta> will call C<Moose::Util::MetaRole::apply_metaroles> and
37e4fe95 719C<Moose::Util::MetaRole::apply_base_class_roles> as needed.
95056a1e 720
721Note that if any of these methods already exist, they will not be
722overridden, you will have to use C<build_import_methods> to get the
723coderef that would be installed.
e606ae5f 724
725This method accepts the following parameters:
726
4b68e0de 727=over 8
e606ae5f 728
5ac14e89 729=item * with_meta => [ ... ]
e606ae5f 730
37e4fe95 731This list of function I<names only> will be wrapped and then exported. The
5ac14e89 732wrapper will pass the metaclass object for the caller as its first argument.
733
734Many sugar functions will need to use this metaclass object to do something to
735the calling package.
e606ae5f 736
737=item * as_is => [ ... ]
738
37e4fe95 739This list of function names or sub references will be exported as-is. You can
740identify a subroutine by reference, which is handy to re-export some other
741module's functions directly by reference (C<\&Some::Package::function>).
e606ae5f 742
37e4fe95 743If you do export some other package's function, this function will never be
744removed by the C<unimport> method. The reason for this is we cannot know if
745the caller I<also> explicitly imported the sub themselves, and therefore wants
746to keep it.
e05fb8ae 747
f88cfe7c 748=item * trait_aliases => [ ... ]
749
bcbb1f7b 750This is a list of package names which should have shortened aliases exported,
f88cfe7c 751similar to the functionality of L<aliased>. Each element in the list can be
752either a package name, in which case the export will be named as the last
753namespace component of the package, or an arrayref, whose first element is the
754package to alias to, and second element is the alias to export.
755
e606ae5f 756=item * also => $name or \@names
757
758This is a list of modules which contain functions that the caller
759wants to export. These modules must also use C<Moose::Exporter>. The
760most common use case will be to export the functions from C<Moose.pm>.
5ac14e89 761Functions specified by C<with_meta> or C<as_is> take precedence over
ae8817b6 762functions exported by modules specified by C<also>, so that a module
763can selectively override functions exported by another module.
e606ae5f 764
765C<Moose::Exporter> also makes sure all these functions get removed
766when C<unimport> is called.
767
768=back
769
f785aad8 770You can also provide parameters for C<Moose::Util::MetaRole::apply_metaroles>
771and C<Moose::Util::MetaRole::base_class_roles>. Specifically, valid parameters
15851b87 772are "class_metaroles", "role_metaroles", and "base_class_roles".
95056a1e 773
4b68e0de 774=item B<< Moose::Exporter->build_import_methods(...) >>
e606ae5f 775
95056a1e 776Returns two or three code refs, one for C<import>, one for
777C<unimport>, and optionally one for C<init_meta>, if the appropriate
778options are passed in.
779
37e4fe95 780Accepts the additional C<install> option, which accepts an arrayref of method
781names to install into your exporting package. The valid options are C<import>,
782C<unimport>, and C<init_meta>. Calling C<setup_import_methods> is equivalent
783to calling C<build_import_methods> with C<< install => [qw(import unimport
784init_meta)] >> except that it doesn't also return the methods.
e606ae5f 785
b360ed32 786The C<import> method is built using L<Sub::Exporter>.
787It can take a hashref of the form C<{into=>$package}> to specify the package
788it operates on.
789
e606ae5f 790Used by C<setup_import_methods>.
791
4b68e0de 792=back
793
e606ae5f 794=head1 IMPORTING AND init_meta
795
37e4fe95 796If you want to set an alternative base object class or metaclass class, see
797above for details on how this module can call L<Moose::Util::MetaRole> for
798you.
799
800If you want to do something that is not supported by this module, simply
801define an C<init_meta> method in your class. The C<import> method that
802C<Moose::Exporter> generates for you will call this method (if it exists). It
803will always pass the caller to this method via the C<for_class> parameter.
e606ae5f 804
805Most of the time, your C<init_meta> method will probably just call C<<
806Moose->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
95056a1e 813Keep in mind that C<build_import_methods> will return an C<init_meta>
814method for you, which you can also call from within your custom
37e4fe95 815C<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
b360ed32 825 # If you don't use goto you have to specify into.
826 $class->$import({into=>caller},...);
37e4fe95 827
828 ...
829 }
830
831 sub unimport { goto &$unimport }
832
833 sub init_meta {
834 my $class = shift;
835
836 ...
837
838 $class->$init_meta(...);
839
840 ...
841 }
95056a1e 842
e606ae5f 843=head1 METACLASS TRAITS
844
845The C<import> method generated by C<Moose::Exporter> will allow the
846user of your module to specify metaclass traits in a C<-traits>
847parameter passed as part of the import:
848
849 use Moose -traits => 'My::Meta::Trait';
850
851 use Moose -traits => [ 'My::Meta::Trait', 'My::Other::Trait' ];
852
853These traits will be applied to the caller's metaclass
854instance. Providing traits for an exporting class that does not create
855a metaclass for the caller is an error.
856
c5fc2c21 857=head1 BUGS
858
859See L<Moose/BUGS> for details on reporting bugs.
860
e606ae5f 861=cut