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